我试图在另一个节点上触发onCreate事件后从一个节点读取数据。来自这些节点的数据随后将被发送到Google表格。虽然在构建函数时未报告任何错误,但存在部署错误,其中显示消息“必须妥善处理承诺”。这是我的功能:
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)
}
})
})
错误消息是针对该行的
acceptRef.once('value').then(async function (snapshot2)
答案 0 :(得分:1)
如documentation中所述,后台函数必须返回一个承诺,该承诺将在所有异步工作完成后解决。这意味着您必须处理异步工作的API调用返回的所有承诺。所有Firebase API以这种方式都是异步的。现在,您的函数将忽略once().then().catch()
返回的promise。仅在promise上调用then
或catch
并不能完全“处理” promise-它们只是附加了回调。
由于您正在使用异步/等待,因此甚至不需要使用then
和catch
。只需await
once()
返回的承诺,然后继续处理结果即可。
const snapshot2 = await acceptRef.once('value');
const price = snapshot2.child('****').val()
...