簡體   English   中英

Firestore承諾后返回

[英]Firestore return after promise

我正在使用Firestore,Cloud Functions和Dialogflow來創建Google Home應用程序。 我的雲函數應該根據集合中的值返回計算,但我只能返回一個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);    

在調用response.send之前,如何等待promise返回實際值? 我還嘗試將response.send放在then中,但該函數完成並在操作完成之前發送頭文件。

如果您正在使用promises,則需要將值作為Promise.then鏈的一部分返回。 這通常是通過將一堆.then()調用/塊鏈接在一起來完成的。 請記住,只有在Promise結算時才會調用then()部分。

在您的情況下,看起來您可以生成balanceresponse並在計算workingbalance后在同一代碼塊內調用response.send() response.send()的調用不需要在函數的末尾。 所以你可能看起來像這樣的塊:

.then(snapshot => {
    var workingbalance = 0
    snapshot.forEach(doc => {
        workingbalance = workingbalance + doc.data().amount;
    });

    balanceresponse = {
      "speech": request.body.result.parameters.accountowner + " has a balance of $" + workingbalance,
      "displayText": request.body.result.parameters.accountowner + " has an account balance of $" + workingbalance,
      "data": {},
      "contextOut": [],
      "source": "system"
    }

    response.send(balanceresponse); 
})

如果由於某種原因,您想要將計算分開並計算不同塊中的balanceresponse ,則可以這樣做。 我不一定推薦這個,但它有一些優點(例如,你可以在每個部分之后放置多個catch塊,我在這里展示):

// query firestore based on user
var transactions = db.collection('accounts')
               .doc(request.body.result.parameters.accountowner)
               .collection('transactions');
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);
    });

    .then( 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);    
    })

    .catch(err => {
        console.log('Error sending response', err);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM