簡體   English   中英

Google Cloud Datastore:一次交易即可查找和更新

[英]Google Cloud Datastore: lookup and update in one transaction

在任務示例之后,我正在關注Google文檔中的Cloud Datastore示例以及Github示例。 我正在嘗試進行單個函數調用,並通過在描述中查找來將任務標記為完成。

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);
  });
}

現在,更新不會發生:(

由於@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);
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM