本文介绍了FieldValue.increment不起作用,但是在字段中添加“操作数".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用firebase数据库和带有新 FieldValue.increment 来增加计数器,但这不起作用,但是会添加"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.

我的功能非常简单:

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

countertest: {
  myusername: 1
}

但我知道

countertest: {
  myusername: {
    operand: 1
  }
}

操作数:1即使我多次调用函数也永远不会递增.

instead and operand: 1 never increments even if I call my function multiple times.

有人可以指出我在这里犯什么错误吗?

Can somebody point out what error I'm making here?

谢谢!

推荐答案

FieldValue.increment()是Cloud Firestore的功能,但您显然正在尝试将其应用于实时数据库.这是行不通的-它们是不同的数据库,并且实时数据库不支持这样的原子增量.

FieldValue.increment() is a feature of Cloud Firestore, but you're apparently trying to apply it to Realtime Database. This is not going to work - they are different databases, and Realtime Database doesn't support atomic increments like this.

您实际上在这里所做的是将返回的FieldValue对象的JSON表示形式写入Realime数据库.显然,在内部,FieldValue对象具有一个称为操作数"的属性,该属性包含要增加的值.

What you're actually doing here is writing the JSON representation of the returned FieldValue object to Realime Database. Apparently, internally, the FieldValue object has a property called "operand" which contains the value to increment by.

这篇关于FieldValue.increment不起作用,但是在字段中添加“操作数".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:42