I'm using Firestore, Cloud Functions and Dialogflow to create a Google Home app. My cloud function should return a calculation based on values in a collection, but I can only return a Promise.
// query firestore based on user
var transactions = db.collection('accounts')
.doc(request.body.result.parameters.accountowner)
.collection('transactions');
var accountbalance = await transactions.get()
.then(snapshot => {
var workingbalance = 0
snapshot.forEach(doc => {
workingbalance = workingbalance + doc.data().amount;
});
return workingbalance
})
.catch(err => {
console.log('Error getting transactions', err);
});
console.log(accountbalance)
balanceresponse = {
"speech": request.body.result.parameters.accountowner + " has a balance of $" + accountbalance,
"displayText": request.body.result.parameters.accountowner + " has an account balance of $" + accountbalance,
"data": {},
"contextOut": [],
"source": "system"
}
response.send(balanceresponse);
How can I wait for the promise to return the actual value before calling response.send? I have also tried to put the response.send inside a then, but the function completes and sends headers before the operation completes.