【问题标题】:Updating Entity in Google Datastore using Nodejs使用 Nodejs 更新 Google Datastore 中的实体
【发布时间】:2017-09-25 20:49:03
【问题描述】:

是否可以更新实体, 仅替换输入中的数据并保留实体的其余部分?

【问题讨论】:

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


    【解决方案1】:

    您需要执行读取实体、更新值、写入实体的事务。

    function updateEntity (updateKey, newValue) {
      const transaction = datastore.transaction();
    
      return transaction.run()
        .then(() => transaction.get(fromKey))
        .then((result) => {
          const entity = result[0];
    
          entity.myProperty = newValue;
    
          transaction.save(
            {
              key: updateKey,
              data: entity
            }
          );
    
          return transaction.commit();
        })
        .catch(() => transaction.rollback());
    }
    

    【讨论】:

    • 我仍然无法进行更新,我得到 ` { "textPayload": "Promise { }", "insertId": "000000-f365b507-72d8-48e2-a3ce-f65052545bb6 ", "resource": { "type": "cloud_function", "labels": { "project_id": "******", "region": "us-central1", "function_name": "UpdateImportLog" } },“时间戳”:“2017-09-25T11:57:45.233Z”,“严重性”:“INFO”,“标签”:{“execution_id”:“xci8wzhqkbqj”},“logName”:“项目/加密货币” -analyzer-180608/logs/cloudfunctions.googleapis.com%2Fcloud-functions", "receiveTimestamp": "***" }`
    【解决方案2】:

    这样我就得到了想要的效果:

       transaction.run(function(err) {
        if (err) {
            res.end(err);
        }
    
        transaction.get(key, function(err, entity) {
            if (err) {
                res.end(err);
            }
            if (entity) {
                entity.ImportStatus = InputImportStatus;
            } else {
                res.end('No Entity err');
            }
    
            transaction.save({
                key: key,
                data: entity
            });
    
            transaction.commit(function(err) {
                if (!err) {
                    res.send(`Updated`)
                } else { res.end(err); }
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2019-10-19
      • 2021-04-05
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多