简体   繁体   English

FieldValue.increment 不起作用但添加“操作数”

[英]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.

相关问题 fieldValue.increment(int) 不会减少 firestore 中的值 flutter - fieldValue.increment(int) does not decrement the value in firestore flutter 谷歌云功能 Firebase 使用 NodeJS 更新 FieldValue.increment(1) - TypeError: FieldValue.increment is not a function - Google cloud functions Firebase Update FieldValue.increment(1) using NodeJS - TypeError: FieldValue.increment is not a function Nodejs/Mocha - FieldValue.increment - FirebaseError:使用无效数据调用函数 DocumentReference.update() - Nodejs/Mocha - FieldValue.increment - FirebaseError: Function DocumentReference.update() called with invalid data Firebase Firestore Increment FieldValue 不递增 - Firebase Firestore Increment FieldValue does not increment Firestore中的Cloud Functions FieldValue增量TypeError - Cloud Functions FieldValue increment TypeError in Firestore FirebaseRealtimeDatabase 的增量操作是如何工作的? - How does the increment operation on FirebaseRealtimeDatabase work? 在 firestore 中“属性‘arrayUnion’在类型‘typeof FieldValue’上不存在” - In firestore "Property 'arrayUnion' does not exist on type 'typeof FieldValue'" 使用 firestore.FieldValue.increment(1) 时如何解决错误“参数“dataOrField”的值不是有效的 Firestore 文档”? - How to resolve error "Value for argument "dataOrField" is not a valid Firestore document" while using firestore.FieldValue.increment(1)? case 操作数类型不匹配 when 子句操作数类型 integer vs boolean athena - case operand type does not match when clause operand type integer vs boolean athena 将 Flutter 中的 FieldValue.arrayUnion 与 Cloud FireStore 一起使用 - Use FieldValue.arrayUnion in Flutter with Cloud FireStore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM