[英]Google Cloud Datastore Update Entity: Node.js Client
How to update GCP datastore entity in nodejs. 如何在Node.js中更新GCP数据存储区实体。
// Saves the entity
datastore
.save(task)
.then(() => {
console.log(`Saved ${task.key.name}: ${task.data.description}`);
})
.catch(err => {
console.error('ERROR:', err);
});
Usually save function is storing the data and if we send the existing the data it is updating fine. 通常,保存功能是存储数据,如果我们发送现有数据,则可以更新。 But if we miss some columns then it is nullifying those columns. 但是,如果我们错过了一些列,那么它将使那些列无效。 Is there any function to update entity in datastore. 是否有任何功能可以更新数据存储区中的实体。
save()
in datastore overwrites the entity. 数据存储区中的save()
覆盖实体。 If you want to do an update : 如果要进行更新:
save()
to update. 使用save()
进行更新。 To avoid race conditions, datastore transaction
can be used 为避免争用情况,可以使用数据存储transaction
What I understand is that you are confused about why updating an entity with, say 8 valid properties, will have 2 properties nulled if you only provide 6/8 properties in the update. 我了解的是,您对为什么要更新一个实体(如果有8个有效属性)将使2个属性为空(如果仅在更新中提供6/8个属性)感到困惑。
This is by design as Datastore
will write the entire entity with save()
. 这是设计Datastore
,因为Datastore
将使用save()
写入整个实体。 What you want to do is force save(method=upsert, yourdata)
, and a shortcut is to use the upsert()
method. 您要执行的操作是强制save(method=upsert, yourdata)
,快捷方式是使用upsert()
方法。
reading the "save" documentation at https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore - 阅读https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore中的“保存”文档-
By default, all properties are indexed. 默认情况下,所有属性均已建立索引。 To prevent a property from being included in all indexes, you must supply an excludeFromIndexes array. 为了防止属性包含在所有索引中,必须提供excludeFromIndexes数组。
I assume that you get null fields because they're indexed. 我假设您得到空字段,因为它们已被索引。 try to remove indexes from fields that may not exist. 尝试从可能不存在的字段中删除索引。
If you use gstore-node there is an update() method on the Model that does this for you. 如果使用gstore-node ,则模型上有一个update()方法可以为您完成此操作。
gstore will do (in a transaction): gstore将执行(在交易中):
Here is the documentation of the update() on Model: https://sebelga.gitbooks.io/gstore-node/content/model/update-method.html 这是有关模型的update()的文档: https : //sebelga.gitbooks.io/gstore-node/content/model/update-method.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.