Reactjs 对firebase云函数进行多返回反应
有没有办法在foreach+ref中设置myReactjs 对firebase云函数进行多返回反应,reactjs,firebase,google-cloud-functions,Reactjs,Firebase,Google Cloud Functions,有没有办法在foreach+ref中设置myupdateQuestions变量?或者我必须执行两种不同的云功能?我想返回一个巨大的更新updateQuestions,但我需要基于firebase数据从foreach返回数据 以下是我的功能: exports.deleteQuestion = functions.database.ref('questions_for_mars/{pushId}').onDelete(event => { const original = even
updateQuestions
变量?或者我必须执行两种不同的云功能?我想返回一个巨大的更新updateQuestions
,但我需要基于firebase数据从foreach返回数据
以下是我的功能:
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)
})
});
您似乎正在使用Firebase SDK的旧版本来实现云功能,请参阅。您必须更新它(参见本文档中的说明) 然后,对于
event.child('answers').forEach(child=>{})
循环,需要使用来管理对数据库的并行异步查询
您将在与实现值对应的数组中收到Promise.all()
的结果,其顺序与查询数组相同
因此,下面的代码应该可以做到这一点(未经测试):
您似乎正在使用Firebase SDK的旧版本来实现云功能,请参阅。您必须更新它(参见本文档中的说明) 然后,对于
event.child('answers').forEach(child=>{})
循环,需要使用来管理对数据库的并行异步查询
您将在与实现值对应的数组中收到Promise.all()
的结果,其顺序与查询数组相同
因此,下面的代码应该可以做到这一点(未经测试):
不工作更新不获取查询请注意,我进行了更新。检查你是否使用了最后一个版本。问题是查询是否为空?@manyouuwx我看你已经接受了我的答案。很高兴我能帮助你!你也可以投票支持我的答案,见。谢谢不工作更新不获取查询请注意,我进行了更新。检查你是否使用了最后一个版本。问题是查询是否为空?@manyouuwx我看你已经接受了我的答案。很高兴我能帮助你!你也可以投票支持我的答案,见。谢谢
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)
});
})