使用 Nodejs 更新 Google 数据存储中的实体

Updating Entity in Google Datastore using Nodejs

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

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

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

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

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