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)
})
});
1 Answer 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
manyouuwx
seems to work thanks ! :) is it a good pratice to do like i did or there is a better way ?
Doug Stevenson
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.
Explore related questions
See similar questions with these tags.
lang-js