【问题标题】:Google Cloud Datastore: lookup and update in one transactionGoogle Cloud Datastore:在一个事务中查找和更新
【发布时间】:2018-02-20 21:55:47
【问题描述】:

我正在关注来自 Google documentationGithub 示例的 Cloud Datastore 示例,以及任务示例。我正在尝试进行单个函数调用,并通过在描述中查找任务将其标记为已完成。

function markDoneByDesc(queryString) {
  const query = datastore
    .createQuery('Task')
    .filter('description', '=', queryString);
  var taskKeyId;

  datastore
  .runQuery(query)
  .then(results => {
    const tasks = results[0];

    console.log('Task found:', tasks[0]);
    // I realize there might be multiple tasks with the same desc,
    // but I want to update just one for now
    taskKeyId = tasks[0][datastore.KEY].id;
    console.log('Saving the task Key ID', taskKeyId);
    return taskKeyId;
  })
  .then((taskKeyId) => {
    console.log('Calling markDone with task Key ID', taskKeyId);
    markDone(taskKeyId); // From the original function in the sample
    console.log('Updated task');
  })
  .catch(err => {
    console.error('ERROR:', err);
  });
}

目前,更新不会发生 :(

【问题讨论】:

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


    【解决方案1】:

    感谢@callmehiphop的帮助,我找到了解决方案!

    看起来我需要将数据存储查询中返回的taskKeyId 转换为整数,然后将其传递给markDone() 函数。否则,它作为字符串传递,并且通过该 ID 键查找失败。

    正确的代码如下所示(注意第一个 return 语句中的 parseInt()):

    function markDoneByDesc(queryString) {
      const query = datastore
        .createQuery('Task')
        .filter('description', '=', queryString);
      var taskKeyId;
    
      datastore
      .runQuery(query)
      .then(results => {
        const tasks = results[0];
    
        console.log('Task found:', tasks[0]);
        // I realize there might be multiple tasks with the same desc,
        // but I want to update just one for now
        taskKeyId = tasks[0][datastore.KEY].id;
        console.log('Saving the task Key ID', taskKeyId);
        return parseInt(taskKeyId,10);
      })
      .then((taskKeyId) => {
        console.log('Calling markDone with task Key ID', taskKeyId);
        markDone(taskKeyId); // From the original function in the sample
        console.log('Updated task');
      })
      .catch(err => {
        console.error('ERROR:', err);
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 2020-12-30
      • 2019-03-22
      • 2021-04-05
      • 1970-01-01
      • 2018-07-27
      相关资源
      最近更新 更多