【问题标题】:Firebase cloud functions error - Function returned undefined, expected Promise or valueFirebase 云函数错误 - 函数返回未定义、预期的 Promise 或值
【发布时间】:2018-12-09 21:21:09
【问题描述】:

我正在尝试在创建新文档时使用 Google Cloud 功能在 Appsflyer 上记录购买事件,但出现此错误。

我的所有插值在我的日志中似乎都很好。 我的功能是

exports.validatePaymentAppsflyer = functions.firestore.document('_orderFinishedSuccessfully/{id}').onCreate((snap, context) => {

    console.log(snap, context);

    const newValue = snap.data();
    const requestData = newValue;
    console.log(requestData.platform);

    if ( requestData.platform === 'ios' ) {
        appId = 'id1303984176';                
    } else {
        appId = 'com.myapp';                
    }

    var request = require("request");

    var options = { method: 'POST',
    url: 'https://api2.appsflyer.com/inappevent/' + appId,
    headers: 
    { 
        "authentication": 'M762jn36Bb7kBt70jNdtrU',
        'Content-Type': 'application/json' 
        },
    body: 
    { appsflyer_id: requestData.appsflyerId,
        customer_user_id: requestData.customerUserId,
        eventName: 'af_purchase',
        eventValue: {
            "af_revenue":requestData.totalTTC,
            "af_order_id":requestData.orderId,
            "af_city":requestData.city,
            "af_date_b":requestData.date
        },
        eventCurrency: 'EUR',
        ip: requestData.userIp,
        eventTime: requestData.date,
        af_events_api: 'true' },
    json: true };

    console.log(options);


    request(options, function (error, response, body) {
    if (error) throw new Error(error);

    console.log(body);
    });
});

我需要你的帮助

【问题讨论】:

    标签: javascript firebase google-cloud-functions appsflyer


    【解决方案1】:

    预计云函数会返回有意义的内容,通常您希望返回 Promise。这样引擎就会知道您的异步操作已经完成,而不必等待超时发生。

    要修复您的代码,只需返回 Promise:

    return new Promise((resolve, reject) => {
        request(options, function (error, response, body) {
            if (error) reject(error);
            else resolve(response);
        });
    });
    

    【讨论】:

    猜你喜欢
    • 2018-12-03
    • 2019-08-12
    • 2018-06-12
    • 2019-08-06
    • 2019-11-03
    • 2018-12-05
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多