[英]FieldValue.increment does not work but adds "operand"
I'm using a firebase database and a simple function with the new FieldValue.increment to increment a counter but that does not work but adds "operand" field without ever incrementing it.我正在使用一个 firebase 数据库和一个简单的 function 和新的FieldValue.increment来增加一个计数器,但这不起作用,而是添加“操作数”字段而不增加它。
My function is super simple:我的 function 超级简单:
exports.updateCounters = functions.https.onRequest((req, res) => {
// grab the parameters.
const username = req.query.username;
var updateObject = { };
updateObject[username] = admin.firestore.FieldValue.increment(1);
admin.database().ref('counterstest').update(updateObject);
});
When I deploy and call this function I would expect to see当我部署并调用这个 function 我希望看到
countertest: {
myusername: 1
}
but I see但我明白了
countertest: {
myusername: {
operand: 1
}
}
instead and operand: 1 never increments even if I call my function multiple times.相反,操作数: 1 即使我多次调用我的 function 也不会增加。
Can somebody point out what error I'm making here?有人能指出我在这里犯了什么错误吗?
Thank you!谢谢!
FieldValue.increment()
is a feature of Cloud Firestore, but you're apparently trying to apply it to Realtime Database. FieldValue.increment()
是Cloud Firestore的功能,但是您显然正在尝试将其应用于实时数据库。 This is not going to work - they are different databases, and Realtime Database doesn't support atomic increments like this. 这是行不通的-它们是不同的数据库,实时数据库不支持这种原子增量。
What you're actually doing here is writing the JSON representation of the returned FieldValue object to Realime Database. 您实际上在这里所做的就是将返回的FieldValue对象的JSON表示形式写入Realime数据库。 Apparently, internally, the FieldValue object has a property called "operand" which contains the value to increment by. 显然,在内部,FieldValue对象具有一个称为“ operand”的属性,其中包含要增加的值。
I know this post is a couple of years old, but database.ServerValue.increment
works with the 11.X firebase-admin
Realtime Database like so:我知道这篇文章已经有几年的历史了,但是database.ServerValue.increment
可以与 11.X firebase firebase-admin
实时数据库一起使用,如下所示:
admin.database().ref('counterstest').update({ myusername: database.ServerValue.increment(1) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.