Firebase函数抛出返回了未定义的、预期的Promise或值



我下面的firebase函数抛出错误function返回了未定义的、预期的Promise或值。我到处都在做退货,不确定为什么它仍然会抛出这个错误

我已经研究过类似的代码示例,不确定它会导致什么错误。

exports.on_order_updated_update_new_vs_repeat_order = functions.database.ref("/orders/{id}")
.onUpdate((change, context) => { 
const newValue = change.after.val();
const oldValue =   change.before.val()
//if order is marked delivered then update the data
if(oldValue.order._orderStatus !== 'Delivered' && newValue.order._orderStatus === 'Delivered'){
//find the uid of the customer 
const uid = newValue.customer._uid
var isOldOrder = false  //to track weather the customer is new or repeat
var db = admin.database();
var ref = db.ref('users')
return ref.child(uid).child('orders').once("value").then(
(resp) => {
const orderKeys = Object.keys(resp.val())
if(orderKeys.length > 1)
isOldOrder = true //existing orders there so just set it to true
var date = new Date()
var begDate = findDayBegninning(date)
var endDate = findDayEnd(date)
var anaRef = db.ref('analytics')
return anaRef.child('newVsRepeatOrders').orderByChild("date").startAt(begDate).endAt(endDate).once("value").then(
(rp) => {
if(rp !== undefined && rp.val() !== null){
const newOldObj = rp.val()
var oldOrderVal = 0
var newOrderVal = 0
if(isOldOrder === true){
oldOrderVal = newOldObj[begDate].oldOrdersCount + 1
newOrderVal = newOldObj[begDate].newOrdersCount
}
return anaRef.child('newVsRepeatOrders/' + begDate).update({"oldOrdersCount": oldOrderVal, "newOrdersCount": newOrderVal}).then(
(resp1) => console.log("updated order count") 
).catch(
(err) => console.error("error in updating old vs new order count:" + err)
)
}else{
console.log("no data found for today so adding first record")
var oldOrderCount = 0
var newOrderCount = 0
if(isOldOrder === true)
oldOrderCount++
else
newOrderCount++
var payload = {
"date" : begDate,
"oldOrdersCount": oldOrderCount,
"newOrdersCount" : newOrderCount
}
return anaRef.child('newVsRepeatOrders/' + begDate).set(payload).then(
(rpp) => console.log("updated newVsRepeatOrders")
).catch(
(err) => console.error("Error updating newVsRepeatOrders::" + err)
)
}
}
).catch(
(err) => console.error("Could not execute path newVsRepeatOrders for the customer uid:" + uid + " error is:" + err)
)
}
).catch(
(err) => console.error("Could not find orders for the customer uid:" + uid + " error is:" + err)
)
}
}
)

请忽略此处的文本。stackoverflow不让我发帖说我只有代码和添加文本。

如果顶级条件为false,则不会从函数返回任何内容。让我压缩一下你的函数,这样你就能看得更清楚:

exports.on_order_updated_update_new_vs_repeat_order = functions.database.ref("/orders/{id}")
.onUpdate((change, context) => { 
const newValue = change.after.val();
const oldValue = change.before.val()
//if order is marked delivered then update the data
if(oldValue.order._orderStatus !== 'Delivered' && newValue.order._orderStatus === 'Delivered'){
// return some promise...
}
// nothing is returned if the above condition was false
}
)

在任何情况下,你都需要归还一些东西。如果您的函数在主条件为false的情况下没有异步工作要做,那么只有return null

为了更好的风格和清晰度,你可能还希望把你的承诺串起来,而不是嵌套起来。嵌套的承诺很难阅读,门楣人可能会抱怨这一点,因为它们也容易出错。

相关内容

  • 没有找到相关文章

最新更新