Firebase云功能无法正常运行?

时间:2019-11-13 20:10:04

标签: javascript firebase google-cloud-functions

我写了一个Firebase云函数,但是当我多次删除时,它只能工作70%!而且很慢吗?我做错了什么??

这是我的功能:

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)

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


    var questions_active = snapshot.val()

    var updateQuestions = {};

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

      var mars = child.key
      updateQuestions['/my_answers/' + mars + '/' + idQuestion] = null
      updateQuestions['/mars/' + mars + '/counter/answers_active'] = questions_active - 1


      console.log('question active', original)
    });



    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

    console.log('UPDAYE', updateQuestions)
    return admin.database().ref().update(updateQuestions)

  })


});

1 个答案:

答案 0 :(得分:1)

您必须从回调函数的顶层返回一个承诺,该承诺仅在所有异步工作完成后才能解决。

return admin.database().ref('counter/questions_active').once(...).then(...)

当可以拆除功能并进行清理时,此诺言会向Cloud Functions发出信号。如果您未正确返回承诺,它将在工作完成之前清除。