【问题标题】:Google datastore entity creation and updateGoogle 数据存储实体创建和更新
【发布时间】:2018-06-11 10:28:45
【问题描述】:

我正在尝试使用此处https://cloud.google.com/datastore/docs/reference/libraries 中的示例代码更新我的数据存储类型中的实体。实际代码是这样的:

/ Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Creates a client
const datastore = new Datastore({
  projectId: projectId,
});

// The kind for the new entity
const kind = 'Task';
// The name/ID for the new entity
const name = 'sampletask1';
// The Cloud Datastore key for the new entity
const taskKey = datastore.key([kind, name]);

// Prepares the new entity
const task = {
  key: taskKey,
  data: {
    description: 'Buy milk',
  },
};

// Saves the entity
datastore
  .save(task)
  .then(() => {
    console.log(`Saved ${task.key.name}: ${task.data.description}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

我尝试使用此代码创建一个新实体。但是,当我运行此代码并检查数据存储控制台时,没有创建实体。此外,我无法更新现有实体。这可能是什么原因?

我正在 Google Cloud Functions 中编写代码。这是我运行此函数时的日志:

 {
 insertId: "-ft02akcfpq"  
 logName: "projects/test-66600/logs/cloudaudit.googleapis.com%2Factivity"  
 operation: {…}  
 protoPayload: {…}  
 receiveTimestamp: "2018-06-15T09:36:13.760751077Z"  
 resource: {…}  
 severity: "NOTICE"  
 timestamp: "2018-06-15T09:36:13.436Z"  
}

{
 insertId: "000000-ab6c5ad2-3371-429a-bea2-87f8f7e36bcf"  
 labels: {…}  
 logName: "projects/test-66600/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
 receiveTimestamp: "2018-06-15T09:36:17.865654673Z"  
 resource: {…}  
 severity: "ERROR"  
 textPayload: "Warning, estimating Firebase Config based on GCLOUD_PROJECT. Intializing firebase-admin may fail"  
 timestamp: "2018-06-15T09:36:09.434Z"  
}

【问题讨论】:

    标签: javascript google-cloud-datastore google-cloud-functions


    【解决方案1】:

    我尝试了相同的代码,它对我有用。但是,我注意到实体出现在 Datastore 中之前存在延迟。要更新和覆盖现有实体,请使用 .upsert(task) 而不是 .save(task) (link to GCP documentation)。您还可以使用.insert(task) 而不是.save(task) 来存储新实体。

    还要检查项目 ID 是否正确,以及您正在检查实体是否正确。

    【讨论】:

    • 感谢菲利普的回复。我尝试了您的两种方法并等待了大约 15 分钟,因为您提到您遇到了延迟。但是,我还没有看到控制台上的变化。会不会是我的 package.json 的问题?
    • 另外,我正在寻找实体“属性”的“描述”框中的更改。这是反映变化的地方吗?
    • 是的,如果您对数据存储区中已存在的实体使用 upsert,则会反映更改。您在数据存储中仍然看不到任何条目吗?您可以像本快速入门中描述的那样从 GCP 控制台手动添加实体吗? cloud.google.com/datastore/docs/quickstart
    • 我尝试手动添加实体,它们确实得到了更新。但是,我的意图是通过 Cloud Functions 中的代码自动执行此操作。你能告诉我你在哪里写代码吗?例如,在您的本地系统上还是在云端?
    • 您在回答中向我发送了此链接:link。我无法弄清楚参数“类别”、“完成”和“优先级”是什么意思。你能解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    相关资源
    最近更新 更多