Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 尝试更新firebase数据库中的值,同时侦听其他位置的更改(云功能)_Node.js_Firebase_Firebase Realtime Database_Google Cloud Functions - Fatal编程技术网

Node.js 尝试更新firebase数据库中的值,同时侦听其他位置的更改(云功能)

Node.js 尝试更新firebase数据库中的值,同时侦听其他位置的更改(云功能),node.js,firebase,firebase-realtime-database,google-cloud-functions,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,我正在尝试更新firebase实时数据库中某个字段的参与者计数。 我的数据库如下所示: 根 |_课程 ||_课程1 ||_描述:一些文字 ||_莱克特:什么 |_团体 ||_课程1 ||_第一组 ||_当前:5 ||_其他-//- |_参与 ||_课程1 ||_someUID:Group1 因此,现在我正在尝试收听有关参与/Course1/someUID的文章,当出现一些记录时,我想在groups/Course1/Group1中将“current”字段更新为6。 到目前为止,我已经提供了: ex

我正在尝试更新firebase实时数据库中某个字段的参与者计数。 我的数据库如下所示: 根 |_课程 ||_课程1 ||_描述:一些文字 ||_莱克特:什么 |_团体 ||_课程1 ||_第一组 ||_当前:5 ||_其他-//- |_参与 ||_课程1 ||_someUID:Group1 因此,现在我正在尝试收听有关参与/Course1/someUID的文章,当出现一些记录时,我想在groups/Course1/Group1中将“current”字段更新为6。 到目前为止,我已经提供了:

exports.afterGroupJoined = functions.database.ref('/participation/{courseId}/{uid}').onWrite((change, context) => {
    const writtenContent = change.after.val(); // groupId
    const courseId = context.params.courseId;
    const uid = context.auth ? context.auth.uid : null;

    admin.database().ref('/groups/' + courseId + '/' + writtenContent).once('value').then(snapshot => {
        const count = snapshot.val();
        const current = count+1;
        console.log('User ' + uid + ' joined group ' + writtenContent + ' in course ' + courseId);
        const promise1 = admin.database().ref('/groups/' + courseId + '/' + writtenContent + '/current').set(parseInt(current));
        return Promise.all([promise1]);
    }).catch(err => {
        console.log(err);
      });
});
但我得到的是groups/Course1/Group1中名为“null”的新子级,其子级“current”的值为“0”: 根 |_团体 ||_课程1 ||_第一组 |||_当前:5 |||_其他-//- ||_空的 ||_电流:0 “current”的值应该是6,但我有了一个新的孩子。 这来自日志:

User fNhAVZUo9QeSbYt0TwBIQmL2mYq1 joined group Group1 in course Course1
Error: Reference.set failed: First argument contains NaN in property 'groups.Course1.Group1.current' at validateFirebaseData

非常感谢您的帮助。

您似乎正在阅读以下JSON:

"Group1": {
  "current": 5
}
这意味着您需要此代码来获取实际计数:

const count = snapshot.val().current;
但是您没有使用组中的任何其他内容,因此您最好只阅读计数本身。因为你只有一个承诺,所以所有的电话似乎都没有必要。所以整个街区可能是:

let ref = admin.database().ref('/groups/' + courseId + '/' + writtenContent+'/current');
ref.once('value').then(snapshot => {
    const count = snapshot.val();
    return ref.set(count+1);
}).catch(err => {
    console.log(err);
});

最后:如果多个用户几乎同时运行上述操作,这种方法很容易受到竞争条件的影响。您可能需要考虑。

看起来计数不是一个数字,因为它没有值,或者因为它不是数值。在从snapshot.val.[object object]读取console.logcount后,您可能需要对其进行操作,以便解释:对象不是数字,因此不能向其添加1。我猜您需要const count=snapshot.val.current;