Firebase 云函数 - snapshot.forEach(...).then 不是函数 [英] Firebase Cloud Functions - snapshot.forEach(...).then is not a function

查看:17
本文介绍了Firebase 云函数 - snapshot.forEach(...).then 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库布局是:

@posts
   @postId_abc
     -total_score: ... // will update from cloud function
     -score_count: ... // will update from cloud function

@scores
   @postId_abc
      -uid_1: 10
      -uid_2: 20
      _uid_3: 50

每当用户设置分数时,我想使用 cloud functionscores 参考中的所有分数相加并将它们设置为 total_score该特定帖子的 属性.当我尝试下面的代码时,我得到了错误:

Whenever a user sets a score I want to use a cloud function to add up all of the scores from the scores ref and set them to the total_score property on that particular post. When I try the following code below I get errors:

FIREBASE 警告:用户回调引发了异常.TypeError:snapshot.forEach(...).then 不是函数来自已完成函数的异常:TypeError:snapshot.forEach(...).then 不是函数

看来我的 snapshot.forEach((child) => { ... }).then(() => { 不起作用,但 scoreCountProperty.set(...) 确实增加了.

It seems my snapshot.forEach((child) => { ... }).then(() => { isn't working but the scoreCountProperty.set(...) does increment.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.updateScore = functions.https.onCall((data, response) => {

    const postId = data.postId;

    const postsRef = admin.database().ref('/posts/' + postId);
    const scoreCountProperty = admin.database().ref('/posts/' + postId + '/' + 'score_count');

    var totalScore = 0.0;

    admin.database().ref('scores').child(postId).once('value', snapshot => {

    if (snapshot.exists()) {

        snapshot.forEach((child) => {

            totalScore += child.val()

        })
        .then(() => { 
                
            console.log('totalScore: ', totalScore);

            return postsRef.set({ "total_score": totalScore });       
        })
        .then(() => { 
                
            return scoreCountProperty.set(admin.database.ServerValue.increment(1));                
        })
        .catch((error) => {
            console.log('ERROR - updateScore Failed: ', error);
        });
    });
});

推荐答案

正如错误消息所说,Snapshot.forEach() 没有返回您可以调用 then 的对象().事实上,我很确定它什么也没有返回.

As the error message says, Snapshot.forEach() doesn't return an object on which you can call then(). In fact, I'm pretty sure it returns nothing at all.

但你并不需要 then(),因为 Snapshot.forEach() 不是同步操作.

But you don't need a then() anyway, since Snapshot.forEach() is not an synchronous operation.

所以这应该是你想要的:

So this should be what you want:

snapshot.forEach((child) => {
    totalScore += child.val()
})
console.log('totalScore: ', totalScore);

return postsRef.set({ "total_score": totalScore }).then(() => { 
    return scoreCountProperty.set(admin.database.ServerValue.increment(1));                
})
.catch((error) => {
    console.log('ERROR - updateScore Failed: ', error);
});

这篇关于Firebase 云函数 - snapshot.forEach(...).then 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆