【发布时间】:2022-01-08 00:27:00
【问题描述】:
我正在尝试运行此代码:
function updateUserLimitations(userId, limitations, batchOrTransaction = undefined) {
const userLimitationsRef = firestore
.collection("users")
.doc(userId)
.collection("limitations")
.doc("userLimitations");
if (batchOrTransaction) {
return batchOrTransaction.set(
userLimitationsRef,
limitations,
{ merge: true }
);
}
return userLimitationsRef.set(limitations, { merge: true });
}
updateUserLimitations(userId, { "messages.totalMessages": admin.firestore.FieldValue.increment(1) });
但是……
而不是在我的数据库中获取此文档数据:
{ // Doc data
messages: {
initialDate: timestamp, // Date of the first message (I need to preserve it),
totalMessages: 20,
},
}
我得到:
{
...other doc data...,
messages.totalMessages: 20,
}
我需要带有合并选项的集合,因为我正在更新并在文档不存在时创建...。
有什么想法吗?我在这里做错了吗?
【问题讨论】:
-
嗨。点表示法仅适用于地图。 “message.totalMesssages”不是地图。你应该这样做:
{ message: {} } message['totalMessages'] = admin.firestore.FieldValue.increment(1)。另外,为什么在申请merge: true时使用它?如果你使用合并,你可以直接写{ message: { totalMessages: 'yourValue' } } -
@LucasDavidFerrero { merge: true } 是否也适用于更新对象的特定字段?我认为这只能通过点符号来实现。
-
limitations包含什么?向我们展示价值。 -
@AlexMamo 我用值更新了问题
-
我认为这可能对你有用@VictorMolina stackoverflow.com/questions/46597327/…
标签: javascript firebase google-cloud-firestore