You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用GetLocations报错“Object does not exist to execute method on”求助

问题分析与解决方案

你遇到的这个SoftLayer_Exception: Object does not exist to execute method on错误,核心原因很明确:SoftLayer_Location_Group的getLocations方法是实例方法,必须先指定具体的位置组ID才能调用。

你的代码只是创建了LocationGroupService对象,但没有告诉API要操作哪一个位置组——就像你要查询某个班级的学生,却没说清楚是哪个班级,系统自然找不到对应的对象来执行操作。

修正方案

方案1:先获取所有位置组,再遍历获取每个组的位置

如果你不确定要操作的位置组ID,可以先调用GetAllObjects()获取所有可用的位置组,再针对每个组调用getLocations:

package main

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

func main() {
    sess := session.New("user", "password")
    
    // 初始化位置组服务
    locationGroupService := services.GetLocationGroupService(sess)
    
    // 获取所有位置组列表
    groups, err := locationGroupService.GetAllObjects()
    if err != nil {
        fmt.Printf("%s\n", err.Error())
        return
    }
    
    // 遍历每个位置组,获取其下的位置信息
    for _, group := range groups {
        fmt.Printf("位置组ID: %d, 名称: %s\n", *group.Id, *group.Name)
        
        // 指定当前位置组的ID,再调用getLocations
        locations, err := locationGroupService.Id(*group.Id).GetLocations()
        if err != nil {
            fmt.Printf("获取该组位置失败: %s\n", err.Error())
            continue
        }
        fmt.Printf("该组包含的位置: %+v\n\n", locations)
    }
}

方案2:直接指定已知的位置组ID

如果你已经知道目标位置组的ID,可以直接通过Id()方法绑定后调用getLocations:

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)
    
    // 替换为你实际需要的位置组ID(比如1是常见的默认组,具体以GetAllObjects的结果为准)
    targetGroupID := 1
    locations, err := locationService.Id(targetGroupID).GetLocations()
    if err != nil {
        fmt.Printf("%s\n", err.Error())
        return
    }
    fmt.Printf("%+v", locations)
}

额外说明

  • 你可以通过GetAllObjects()的结果,找到符合你需求的位置组ID;
  • 如果后续仍然报错,需要检查你的API密钥是否拥有访问位置组资源的权限,但当前错误的核心原因是未指定位置组ID。

内容的提问来源于stack exchange,提问作者Dennis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:18:41