【问题标题】:why am i getting this error in Firebase "Function returned undefined, expected Promise or value"为什么我在 Firebase 中收到此错误“函数返回未定义、预期的承诺或值”
【发布时间】:2018-10-30 01:56:34
【问题描述】:

我在我的 firebase 后端 Nodejs 代码中收到此错误“函数返回未定义、预期的承诺或值” 这是正在运行的代码;

exports.payOnDelivery = functions.database.ref('/CompletedJobs/{id}').onCreate((snapshot, context) => {
let chargeID = snapshot.child("chargeID/id").val();
let amount = +(snapshot.child("chargeID/amount").val());
let amountAfterCut = amount*0.75;
let emailHash = snapshot.child("jobTaker").val();
if (chargeID == null || amount == null || emailHash == null){
    console.log("Could not parse data");
    return 1;
}
console.log("This is Before Observe");
console.log('Blah blah:', chargeID, amount, emailHash);
admin.database().ref(`Couriers/${emailHash}`).once('value').then(function(userSnapshot){
    const accountID = userSnapshot.child("stripeAccount/id");
    console.log("AcctID:",accountID);
    stripe.transfers.create({
        amount: amountAfterCut,
        currency: "cad",
        source_transaction: chargeID,
        destination: accountID
    }), function(err, transfer){
        if (err){
            console.log(err);
            return 1;
        }
        else{
            console.log("Transfer made",transfer);
            return 0;
        }
    }
}, function(error){
    console.error(error);
});
});

非常感谢任何帮助。 谢谢。

【问题讨论】:

标签: node.js firebase firebase-realtime-database stripe-payments google-cloud-functions


【解决方案1】:

您需要从数据库查询中返回 Promise。

变化:

admin.database().ref(`Couriers/${emailHash}`).once('value')...

到:

return admin.database().ref(`Couriers/${emailHash}`).once('value')....

更新:

正如 cmets 所指出的,此答案并未解决所有问题。我不精通条纹。希望其他人会提供更好的答案。试试看sample code here

【讨论】:

  • 这还不够——then 回调中嵌入了异步工作。
  • 我尝试了返回版本,这似乎摆脱了错误消息,但没有调用 then 回调中的其余代码
猜你喜欢
  • 2019-06-09
  • 2018-06-17
  • 2019-05-11
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 2018-04-18
  • 2018-12-03
相关资源
最近更新 更多