1

I writed a firebase cloud functions but its working only 70% when i do multiple delete ! and it's very slow ? what im doing wrong ??

here is my function :

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)
 })
});
Doug Stevenson
320k37 gold badges458 silver badges474 bronze badges
asked Nov 13, 2019 at 20:10

1 Answer 1

1

You're obliged to return a promise from the top level of your callback function that resolves only after all the asynchronous work is complete.

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

This promise signals to Cloud Functions when it's OK to tear down the function and clean up. If you don't return a promise correctly, it will clean up before the work is complete.

answered Nov 13, 2019 at 20:29
Sign up to request clarification or add additional context in comments.

2 Comments

seems to work thanks ! :) is it a good pratice to do like i did or there is a better way ?
You can do it any way you want, so long as the function returns a promise that resolves only after all the work is complete.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.