【问题标题】:Deleting entities from the Google Cloud Datastore从 Google Cloud Datastore 中删除实体
【发布时间】:2022-02-23 13:50:52
【问题描述】:

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

我只按照this documentation获取密钥:

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

运行查询:

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

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

datastore.delete(keys);

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

如果我在运行查询后立即执行 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() 期望的数组吗?

【问题讨论】:

  • 你能分享整个错误的踪迹吗?
  • InvalidKey:密钥至少应包含一种。在 Object.keyToKeyProto (/node_modules/@google-cloud/datastore/build/src/entity.js:974:19) 在 /node_modules/@google-cloud/datastore/build/src/request.js:194:45 在Array.map () 在 Datastore.delete (/node_modules/@google-
  • cloud/datastore/build/src/request.js:192:37) at /node_modules/@google-cloud/promisify/build/src/index.js:57:28 at new Promise ( ) 在 Datastore.wrapper (/node_modules/@google-cloud/promisify/build/src/index.js:42:16) 在 /app.js:103:27 在 processTicksAndRejections (internal/process/task_queues.js :95:5)

标签: node.js google-cloud-platform datastore


【解决方案1】:

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

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

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

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

datastore.delete(justTheKeys);

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 2020-01-22
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2018-07-16
    • 1970-01-01
    相关资源
    最近更新 更多