Firebase函数抛出未定义的预期承诺或值

时间:2018-12-26 20:09:33

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

我的以下firebase函数引发错误函数返回未定义,预期的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不允许我发表帖子说我只有代码并添加文本。

1 个答案:

答案 0 :(得分:3)

如果顶级条件为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
    }
)

在每种情况下您都需要退货。如果您的函数在主要条件为假的情况下没有异步工作,只需return null

作为更好的样式和清晰度,您可能还希望将您的诺言链接起来而不是嵌套它们。嵌套的诺言很难阅读,而小子可能会抱怨,因为它们也容易出错。