gpt4 book ai didi

node.js - 从 Google Cloud Datastore 中删除实体

转载 作者:行者123 更新时间:2023-12-03 08:09:45 25 4
gpt4 key购买 nike

我在使用 node.js 从 Google Cloud Datastore 中删除实体时遇到问题。我怀疑我错过了一些非常基本的东西,因为这应该不难。

我仅按照 this documentation 获取 key :

const query = datastore.createQuery('coin').select('__key__');

运行查询:

const [keys] = await datastore.runQuery(query);

按照 this documentation 键删除生成的实体:

datastore.delete(keys);

但我得到“InvalidKey: key 应该至少包含一种。”

如果我在运行查询后立即执行 console.log(keys) ,确实会出现一个看似有效类型的关键结果的数组:

 [
{
[Symbol(KEY)]: Key {
namespace: undefined,
id: '5083500323536896',
kind: 'coin',
path: [Getter]
}
},
{
[Symbol(KEY)]: Key {
namespace: undefined,
id: '5130717650485248',
kind: 'coin',
path: [Getter]
}
},
etc...

上面的不是数组 .delete() 所期望的吗?

最佳答案

感谢this post我意识到仅键查询的结果不是键数组,而是存储在符号属性下的键数组。现在当我查看输出时,这一点很明显,但它太出乎我的意料,我没有想到。

因此,为了对键数组运行 delete(),我需要从每个结果中提取键,如下所示:

const justTheKeys = keys.map(function(res) {
return res[datastore.KEY];
});

然后我终于可以删除实体了:

datastore.delete(justTheKeys);

关于node.js - 从 Google Cloud Datastore 中删除实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71169244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com