未处理的Google Cloud Function拒绝

时间:2019-03-17 06:16:27

标签: javascript node.js react-native promise google-cloud-platform

我正在使用Google Cloud函数来处理数据。在我的功能之一中,我正在嵌套.then。

问题是,一切正常,但我在功能日志中遇到“未处理的拒绝”错误。

我不知道我在做什么。这是我的云函数代码:

const admin = require('firebase-admin');
const rp = require('request-promise');

module.exports = function(req, res) {

const phone = String(req.body.phone).replace(/[^\d]/g, '');
const amount = parseInt(req.body.amount);
const couponCodeName = (req.body.couponCodeName);
const couponUsage = parseInt(req.body.couponUsage);
const usersCouponUsage = parseInt(req.body.usersCouponUsage);
const finalAddress = (req.body.finalAddress);
const planName = (req.body.planName);
const saveThisAddress = (req.body.saveThisAddress);
const orderNumber = (req.body.orderNumber);

const options = {
    method: 'POST',
    uri:`https://....`,
    body: {
        amount
    },
    json: true
};

return admin.auth().getUser(phone)
        .then(userRecord => {
            return rp(options)
        })
        .then((orderResponse) => {
            return admin.database().ref('trans/'+ phone)
                .push({ amount: orderResponse.amount })
            })
        .then(() => {
            return admin.database().ref('ordersOfUsers/'+ phone)
                .push({ amount })
        })
        .then(() => { 
            return saveThisAddress === true ? 
                admin.database().ref('SavedAddress/'+phone)
                    .push({address: finalAddress}) : null
        })
        .then(() => {
            return admin.database().ref('delivery/'+phone+'/'+orderNumber)
                .set({ plan: planName === "" ? "Single Day Plan" : planName, delivered: false}, () => {
                    res.status(200).send({ success:true })
                })
        })
        .then(() => {
            return couponCodeName === "" ? null : 
                admin.database().ref(`couponCodes/${couponCodeName}`)
                    .update({couponUsage: couponUsage + 1 })
        })
        .then(() => {
            return usersCouponUsage === "" ? null : 
                admin.database().ref(`couponUsage/${phone}`)
                    .update({ [couponCodeName]: usersCouponUsage + 1 })
        })
        .catch((err) => {
            res.status(422).send({ error: err })
        })    
    .catch((err) => {
       res.status(422).send({error: err });
    });
}

1 个答案:

答案 0 :(得分:0)

您的代码在promise中已经具有return语句(catch块中除外)。因此,可以从 return admin.auth()。getUser(phone)中删除收益可以解决您的错误。