javascript React firebase云函数具有许多返回值

xt0899hw  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(179)

有没有办法在foreach + ref中设置我的updateQuestions变量?或者我必须做两个不同的云函数?我想返回一个巨大的更新updateQuestions,但我需要从一个foreach基于一个firebase数据返回数据。
以下是我的函数:

exports.deleteQuestion = functions.database.ref('questions_for_mars/{pushId}').onDelete(event => {
      const original = event.val()
      idQuestion = event.key
      authorQuestion = original.author
      //console.log('event', original.answers)

      return admin.database().ref('counter/questions_active').once('value').then((snapshot) => {

        var questions_active = snapshot.val()

        var updateQuestions = {};


        updateQuestions['/counter/questions_active'] = questions_active - 1
        updateQuestions['/my_questions/' + authorQuestion + '/' + idQuestion] = null
        updateQuestions['/my_questions_send/' + authorQuestion + '/' + idQuestion] = null
        updateQuestions['/questions/' + idQuestion] = null
        //updateQuestions['/my_answers/' + authorQuestion + '/' + idQuestion] = null


        event.child('answers').forEach(child => {

          var mars = child.key

          return admin.database().ref('/mars/' + mars + '/counter/answers_active').once('value').then((snapshot) => {

            var answers_active = snapshot.val()

            updateQuestions['/my_answers/' + mars + '/' + idQuestion] = null
            updateQuestions['/mars/' + mars + '/counter/answers_active'] = answers_active - 1

          });

          console.log('TOTO', updateQuestions)
        });
        console.log('UPDAYE', updateQuestions)

        return admin.database().ref().update(updateQuestions)

      })

    });
yhived7q

yhived7q1#

您似乎正在使用旧版本的Firebase SDK for Cloud Functions,请参阅https://firebase.google.com/docs/functions/beta-v1-diff。你一定要更新它(请参阅本文档中的说明)。
然后,对于event.child('answers').forEach(child => {})循环,需要使用Promise.all()来管理对数据库的并行异步查询。
您将在一个数组中接收Promise.all()的结果,该数组对应于与查询数组相同顺序的实现值。
因此,下面的代码应该可以完成这个任务(未经测试):

exports.deleteQuestion = functions.database.ref('questions_for_mars/{pushId}').onDelete((snapshot, context) => {
    const original = snapshot.val()
    idQuestion = snapshot.key
    authorQuestion = original.author
    //console.log('event', original.answers)

    var updateQuestions = {};

    return admin.database().ref('counter/questions_active').once('value')
        .then((snapshot) => {

            var questions_active = snapshot.val();

            updateQuestions['/counter/questions_active'] = questions_active - 1
            updateQuestions['/my_questions/' + authorQuestion + '/' + idQuestion] = null
            updateQuestions['/my_questions_send/' + authorQuestion + '/' + idQuestion] = null
            updateQuestions['/questions/' + idQuestion] = null
            //updateQuestions['/my_answers/' + authorQuestion + '/' + idQuestion] = null

            var queries = [];

            snapshot.child('answers').forEach(child => {
                var mars = child.key
                queries.push(admin.database().ref('/mars/' + mars + '/counter/answers_active').once('value'))
            });

            return Promise.all(queries);
        })
        .then(results => {
            results.forEach(snapshot => {
                var answers_active = snapshot.val()
                //Here you need to extract the value of mars from snapshot.key
                //I let you do this piece of code!! 
                updateQuestions['/my_answers/' + mars + '/' + idQuestion] = null
                updateQuestions['/mars/' + mars + '/counter/answers_active'] = answers_active - 1
            });
            return admin.database().ref().update(updateQuestions)
        });

})

相关问题