简体   繁体   English

必须在 Firebase CLI 数据请求中适当处理承诺

[英]Promises must be handled appropriately in Firebase CLI data request

I am trying to read data from a node after an onCreate event has been triggered in another node.在另一个节点中触发 onCreate 事件后,我试图从一个节点读取数据。 Data from these nodes would then be sent to a Google Sheet.然后将来自这些节点的数据发送到 Google 表格。 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.虽然我在构建 function 时没有报告错误,但出现了一个部署错误,其中显示了消息“必须正确处理承诺”。 Here is my function:这是我的 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)

As explained in the documentation , background functions must return a promise that resolves when all asynchronous work is complete.正如 文档中所解释的,后台函数必须返回一个 promise,当所有异步工作完成时它会解析。 This means that you must deal with any promises returned by API calls that work asynchronously.这意味着您必须处理异步工作的 API 调用返回的任何承诺。 All Firebase APIs are asynchronous in this way.所有 Firebase API 都是以这种方式异步的。 Right now, you function ignores the promise returned by once().then().catch() .现在,您 function 忽略了由once().then().catch()返回的 promise 。 Simply calling then or catch on the promise doesn't fully "handle" the promise - they are just attaching callbacks.简单地调用thencatch promise 并不能完全“处理” promise - 它们只是附加回调。

Since you are using async/await, there is no need to even use then and catch .由于您使用的是 async/await,因此甚至不需要使用thencatch Just await the promise returned by once() and continue dealing with the result.只需awaitonce()返回的 promise 并继续处理结果。

const snapshot2 = await acceptRef.once('value');
const price = snapshot2.child('****').val()
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM