简体   繁体   English

从 Google Cloud Datastore 中删除实体

[英]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.

相关问题 从 Google Cloud Storage 中删除大文件夹 - Deleting a large folder from Google Cloud Storage 使用 fsspec 从 Google Cloud 删除文件 - Deleting a file from Google Cloud using fsspec 删除 Google Cloud Datastore 索引 - Google Cloud Datastore index removal 谷歌云数据存储查询 - Google Cloud Datastore Query's Google App Engine DataStore - 如何以有效的方式从 Java 中子表的键中获取 select 父实体? - Google App Engine DataStore - How to select parent entities from keys of a child table in Java in an efficient way? 即使已设置,也找不到 Google Cloud Datastore 索引 - Google Cloud Datastore Index not found even if it is set 此项目设置为在 Datastore 模式下使用 Cloud Firestore。 此模式只能从 Google Cloud Platform 访问 - This project is set up to use Cloud Firestore in Datastore mode. This mode can only be accessed from Google Cloud Platform 如何读取和写入谷歌云数据存储? - How do I read and write to the google cloud datastore? 如何将 cloud.google.com/go/datastore 与 AppEngine 一起使用? - How to use cloud.google.com/go/datastore with AppEngine? Google Cloud Datastore 和服务帐户:限制访问权限 - Google Cloud Datastore and service accounts: limit access permissions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM