Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google数据存储无效密钥错误_Go_Google Cloud Datastore_Google Cloud Firestore - Fatal编程技术网

Google数据存储无效密钥错误

Google数据存储无效密钥错误,go,google-cloud-datastore,google-cloud-firestore,Go,Google Cloud Datastore,Google Cloud Firestore,我正在尝试使用golang将一个项目放入Google数据存储 我总是遇到一个数据存储:无效的密钥错误,但无法找出这里的错误。我正在使用cloud.google.com/go/datastore包 首先,我尝试获取父节点的密钥,但不确定这是正确的方法,但我最终得到了一个datastore.key作为parentKey 当现在使用parentKey作为父项创建一个新密钥,然后尝试使用这个新密钥放置该项时,我会收到无效密钥错误消息 q := datastore.NewQuery("Supplier")

我正在尝试使用golang将一个项目放入Google数据存储

我总是遇到一个数据存储:无效的密钥错误,但无法找出这里的错误。我正在使用cloud.google.com/go/datastore包

首先,我尝试获取父节点的密钥,但不确定这是正确的方法,但我最终得到了一个datastore.key作为parentKey

当现在使用parentKey作为父项创建一个新密钥,然后尝试使用这个新密钥放置该项时,我会收到无效密钥错误消息

q := datastore.NewQuery("Supplier")
              .Namespace("inventory")
              .Filter("Name =", "supplier-01")
              .Limit(1)
var s []supplier
parentKey, err := client.GetAll(ctx, q, &s)
if err != nil || len(parentKey) < 1 {
    fmt.Printf("could not find parent key: %v\n", err)
    return
}
newKey := datastore.IncompleteKey("InventoryItem", parentKey[0])
//newKey := datastore.NameKey("InventoryItem", item.Name, parentKey[0])
if _, err := client.Put(ctx, newKey, &item); err != nil {
    fmt.Printf("could not save item: %v\n", err)
    return
}
我用NameKey和incomplete键都试过了,但都没有成功


很明显,我在这里遗漏了一些东西,但无法确定它是什么,以及如何将我的项目作为另一个节点的子节点写入数据存储。

Tommy,你在评论中明确了这一点。您需要在新密钥上设置名称空间。我在中找不到隐式执行此操作的方法,因此您必须在调用Put之前执行newKey.namespace=parentKey.namespace。

您检查了吗?我看到了,但在这里不适用,他试图将子项写入其不完整的键,我无法使用不完整的键本身写入项。是否为父项[0]完整的密钥本身?您需要在某个时候打印出newKey,以查看传递给Put的内容。据我所知,它看起来不错@JimMorrison。新建键:/Inventory,5634161670881280/供应商,Supplier-01/库存项,0。但是,我确实意识到,名称空间不是在newKey上设置的,而是在parentKey上设置的。这可能是问题所在吗?我的理解是,父键将隐式地为新实体设置名称空间,但也许我必须显式地这样做?谢谢Jim,从文档判断,我认为这可以通过父键名称空间隐式地完成,或者像查询等操作一样,有一个适当的方法公开在新键上设置名称空间。