douxi8759 2018-05-30 15:34
浏览 57
已采纳

GetLocations失败,并显示“对象不存在,无法在其上执行方法”

I am having trouble with the GetLocations call. Every time I try to execute it I receive the error:

SoftLayer_Exception: Object does not exist to execute method on. (SoftLayer_Location_Group::getLocations) (HTTP 200)

This makes me think that there is something wrong with the locationService object I created but I don't understand what. Does anyone see the issue?

package main

import (
    "fmt"
    "github.com/softlayer/softlayer-go/session"
    "github.com/softlayer/softlayer-go/services"
)

func main() {
    sess := session.New("user", "password")
    locationService := services.GetLocationGroupService(sess)
    locations, err := locationService.GetLocations()
    if err != nil {
        fmt.Printf("%s
",err.Error())
        return
    }
    fmt.Printf("%+v", locations)
}
  • 写回答

1条回答 默认 最新

  • douya1855 2018-05-30 16:39
    关注

    The error that you got is because you need to use an identifier locationGroup ID.

    Add this locationGroupId to you go code, like this example:

    locationGroupId := 1
    
    // Create a session
    sess := session.New(username, apikey)
    
    // Get SoftLayer_Location_Group
    service := services.GetLocationGroupService(sess)
    
    result, err := service.Id(locationGroupId).GetLocations()
    

    Reference:

    https://softlayer.github.io/reference/services/SoftLayer_Location_Group/getLocations/

    To get all locationGroupIds available you can use this go code example:

    package main
    
    /*
    GetAllObjects
    
    Retrieve all locationGroup objects.
    
    Important manual pages:
    https://softlayer.github.io/reference/services/SoftLayer_Location_Group/
    https://softlayer.github.io/reference/services/SoftLayer_Location_Group/getAllObjects/
    
    License: http://sldn.softlayer.com/article/License
    Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
    */
    
    import (
        "fmt"
        "github.com/softlayer/softlayer-go/services"
        "github.com/softlayer/softlayer-go/session"
        "encoding/json"
    )
    
    func main() {
        // SoftLayer API username and key
        username := "set me"
        apikey   := "set me"
    
        // Create a session
        sess := session.New(username, apikey)
    
        // Get SoftLayer_Account service
        service := services.GetLocationGroupService(sess)
    
        result, err := service.GetAllObjects()
        if err != nil {
            fmt.Printf("
     Unable to retrieve all locationGroups:
     - %s
    ", err)
            return
        }
        // Following helps to print the result in json format.
        jsonFormat, jsonErr := json.MarshalIndent(result,"","     ")
        if jsonErr != nil {
            fmt.Println(jsonErr)
            return
        }
        fmt.Println(string(jsonFormat))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看