javascript - 如何使用 firebase 增量功能?
问题描述
我正在寻找一种方法,当任何文档添加到 firestore 集合中时,它会增加文档中的键。
我的代码:
firestore().collection('booking').add({
bookingTime: bookingDate,
userId: firebase.auth().currentUser.uid,
timeslotId: selectedTimeSlot.timeslotId,
bookingId: getBookingId(),
appoitmentTime : selectedTimeSlot.displatString,
age : info.age,
gender : info.gender,
name : info.name,
phonenumber : info.phonenumber,
})
.then(docRef => {
console.log("Document written with ID: ", docRef.id);
firestore().collection('booking').doc(docRef.id).update({numberOfAppoitment: firebase.firestore.FieldValue.increment(1)});
})
.catch(error => {
console.error("Error adding document: ", error);
})
当前numberOfAppoitment
,它只添加 1。我希望随着新数据的添加而增加一些东西
解决方案
我认为您不能在更新时增加没有整数值的字段。FieldValue.increment()
如果您想稍后使用,您应该使用初始值 0 填充该字段。
firestore().collection('booking').add({
numberOfAppoitment: 0,
// include the other fields here...
})
您可能还想将该字段的拼写正确地考虑为“nubmerOfAppointment”。
推荐阅读
- jquery - 模态打开jQuery时标记复选框
- twilio - Studio 流程 -> 发送消息 -> 功能(电子邮件)
- elsa-workflows - Elsa Workflow - 运行时的审批者
- git - Git Checkout Vs Git Checkout -B 与上游仓库
- android - 是否有任何 Android 设备无法从 GNSS 数据中获取速度?
- c++ - 当两个对象共享同一个地址时如何处理?
- saml - 响应没有任何可以通过主题验证错误的有效断言
- angular - npm i 给出代码:'ETIMEDOUT',errno:'ETIMEDOUT',系统调用:'connect',类型:'system' 错误
- grpc - grpc java中的低级事件
- java - 嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造实例