Reputation: 285
I am trying to read data from a node after an onCreate event has been triggered in another node. Data from these nodes would then be sent to a Google Sheet. While there is no error reported when I build the function, there is a deployment error where the message "Promises must be handled appropriately" is shown. Here is my function:
export const copyAcceptanceToSheet = functions.database.ref('/****/{****Id}/****').onCreate(async (snapshot, context) => {
const orderId = snapshot.ref.parent?.key
const pharmacy = snapshot.val()
const timeCreated = new Date
const ****Base = admin.database()
const acceptRef = ****Base.ref('/*****/'+orderId+'/*****'+pharmacy);
acceptRef.once('value').then(async function (snapshot2) {
try{
const price = snapshot2.child('****').val()
await jwtAuthPromise
await sheets.spreadsheets.values.append({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: '****Update!A:D', // update this range of cells
valueInputOption: 'RAW',
requestBody: { values: [[timeCreated, price, pharmacy]]}
}, {})
}
catch(error){
console.log(error)
}
})
})
The error message is for the line
acceptRef.once('value').then(async function (snapshot2)
Upvotes: 0
Views: 121
Reputation: 317372
As explained in the documentation, background functions must return a promise that resolves when all asynchronous work is complete. This means that you must deal with any promises returned by API calls that work asynchronously. All Firebase APIs are asynchronous in this way. Right now, you function ignores the promise returned by once().then().catch()
. Simply calling then
or catch
on the promise doesn't fully "handle" the promise - they are just attaching callbacks.
Since you are using async/await, there is no need to even use then
and catch
. Just await
the promise returned by once()
and continue dealing with the result.
const snapshot2 = await acceptRef.once('value');
const price = snapshot2.child('****').val()
...
Upvotes: 1