Я пытаюсь получить данные из firebase, используя облачные функции, но каждый раз, когда я получаю null в ответ. Однако я узнал, что я могу получить данные, но после завершения выполнения функции. (console.log () печатает значения после завершения функции)
exports.getChatRecord = functions.https.onRequest(async(request, response) => {
const uid:string = request.body.uid
if (uid === undefined){
return response.send("1313")
}
const ref = admin.database().ref('user_chat_record').child(uid)
var ref1
try {
const allMessages = new Array()
await ref.once('value', async function(snapShot:DataSnapshot){
await snapShot.forEach(function(snap:DataSnapshot){
const match_uid = snap.key
let chat_key:string
if (match_uid.localeCompare(uid) < 0){
chat_key = match_uid+uid; // keep match uid in front
}
else chat_key = uid+match_uid;
console.log(chat_key)
ref1 = admin.database().ref('chats').child(chat_key)
ref1.once('value', async function(shot:DataSnapshot){
console.log(shot.val()) // it prints data but after function execution finishes
await allMessages.push(shot.val()) // here not able to get data
})
return false
})
})
return response.json(allMessages)
}
catch (err){
return response.send("error "+err)
}
});