[英]Deleting entities from the Google Cloud Datastore
I'm having trouble deleting entities from the Google Cloud Datastore using node.js. I suspect I'm missing something really basic because this should not be hard.我在使用 node.js 从 Google Cloud Datastore 中删除实体时遇到问题。我怀疑我遗漏了一些非常基本的东西,因为这应该不难。
I get the keys only as per this documentation :我仅根据此文档获得密钥:
const query = datastore.createQuery('coin').select('__key__');
Run the query:运行查询:
const [keys] = await datastore.runQuery(query);
Delete the resulting entities by key as per this documentation :根据本文档按键删除生成的实体:
datastore.delete(keys);
But I get "InvalidKey: A key should contain at least a kind."但是我得到“InvalidKey:一个密钥应该至少包含一种”。
If I do console.log(keys) right after running the query there is indeed an array of what appear to be key results with a valid kind:如果我在运行查询后立即执行 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...
Is the above not the array.delete() is expecting?以上不是 array.delete() 所期望的吗?
Thanks to this post I realized the results of a keys only query are not an array of keys, but are rather an array of keys stored under a symbol property.感谢这篇文章,我意识到仅键查询的结果不是键数组,而是存储在符号属性下的键数组。 It's obvious now when I look at the output, but it was so unexpected I didn't think of it.现在看output就很明显了,但是没想到太意外了。
So in order to run delete() on an array of keys I need to extract just the key from each result like this:因此,为了在键数组上运行 delete(),我需要像这样从每个结果中提取键:
const justTheKeys = keys.map(function(res) {
return res[datastore.KEY];
});
Then I can finally delete the entities:然后我终于可以删除实体了:
datastore.delete(justTheKeys);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.