douxi8759 2018-05-30 07: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 08: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部