云数据存储插入和Upsert问题

问题描述 投票:0回答:1

在实体键下插入其他属性时,我遇到了一个奇怪的问题。

enter image description here

我试图添加一个属性而不修改现有属性,但是,我的下面的代码创建新属性并删除现有数据(显示空白)根据GCP文档,我尝试使用插入和upsert没有问题没有修复

server.post('/submit', (req, res,) => {


    const OutTime  = new Date();

 const key = datastore.key([VData, 5717023518621696])
 const entity = {
   key:key,
   method: 'insert',
   data: { content: OutTime },
 }

 datastore.insert(entity).then(() => {
  // Task inserted successfully.
});
// [END datastore_upsert]

});

https://cloud.google.com/datastore/docs/concepts/entities

node.js google-cloud-datastore
1个回答
2
投票

这是预期的,您不能仅添加/修改现有实体的属性 - 需要重写整个实体。所以你需要get实体,添加新的属性然后update / upsert它回到数据存储区。

来自您引用的文档中的Updating an entity(强调我的):

提供的数据会覆盖现有实体。必须将整个对象发送到Cloud Datastore。如果实体不存在,则更新将失败。如果要更新或创建实体,请使用前面所述的upsert。使用transaction允许您在单个原子事务中执行getupdate操作。

来自Creating an entity(强调我的):

您可以使用upsert将实体保存到Cloud Datastore(如果实体已存在于Cloud Datastore中,则会覆盖该实体)

© www.soinside.com 2019 - 2024. All rights reserved.