另一个Google Firebase错误“函数返回了未定义,预期的承诺或价值”

时间:2018-12-10 21:22:42

标签: javascript firebase google-cloud-firestore google-cloud-functions

我已经阅读了所有相关问题,并观看了有关Probase的Firebase视频,但仍然无法摆脱上述错误。附加的代码在本地运行良好,但是从云功能完成后,我继续收到此错误。任何帮助将不胜感激!

exports.enquiry = functions.firestore.document('users/{userId}/enquiries/{enquiryId}')
    .onCreate((snap, context) => {
        let enquiryDoc = snap.data();
        let {
            uid,
            type
        } = enquiryDoc;



        function ResponseGeneration(type) {
            switch (type) {
                case 'type1':
                    {
                        Response = 5;
                        return Promise.resolve(Response);
                    }

                case 'type2':
                    {
                        var body = `<QUOTA><DATE>20181203</DATE></QUOTA>`;

                        const call = request.post({
                                url: "xxx",
                                port: 443,
                                method: "POST",
                                headers: {
                                    'Content-Type': 'text/xml',
                                },
                                body: body,
                                json: false
                            })
                            .then((parsedBody) => {
                                console.log(parsedBody.statusCode);
                                console.log(parsedBody);
                                return xml2js(parsedBody);
                            })
                            .then((js) => {
                                console.log('JSON.stringify(js):', JSON.stringify(js));
                                var json = JSON.stringify(js);
                                var jsonParsed = JSON.parse(json);
                                if (jsonParsed.hasOwnProperty('Error')) {
                                    Response = jsonParsed['Error']['Message'];
                                } else {
                                    Response = jsonParsed['RESULT'];
                                }
                                console.log("Response: ", Response);

                                return Promise.resolve(Response);

                            }).catch(function(err) {
                                console.log("error from post:", err);
                                // return Promise.resolve(err);
                            });

                        return Promise.all(call); //also tried with Promise.resolve
                    }

                default:
                    return Promise.resolve(0.0);
            }
        }

        ResponseGeneration(type).then((response) => {
            return snap.ref.update({
                quote: response,
                status: 'quoted',
                quoteId: context.params.enquiryId
            });
        });
    });

如您所见,它随处返回Promises,但Firestore控制台继续打印:

Function returned undefined, expected Promise or value

我不太清楚它可能还没有兑现承诺。有帮助吗?

1 个答案:

答案 0 :(得分:1)

onCreate()调用的顶级函数没有返回任何内容,因此会返回错误。

您需要将通话中的Promise返回到ResponseGeneration

    return ResponseGeneration(type).then((response) => {
        return snap.ref.update({
            quote: response,
            status: 'quoted',
            quoteId: context.params.enquiryId
        });
    });