使用请求库的Firebase函数未触发 [英] Firebase Function using request library not firing

查看:63
本文介绍了使用请求库的Firebase函数未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎在那里,但是由于某种原因,我的HTTP发布请求没有触发,最终导致函数超时.完全在我自己旁边并发布我的代码,以查看是否有人接受了我完全缺少的任何菜鸟动作.注意:数据库写入已完成,因此我假设未触发HTTP Post请求,这是一个安全的假设吗?还是JS是另一种野兽?

Almost there, but for some reason my HTTP post request isn't firing and eventually the function timesout. Completely beside myself and posting my code to see if anyone picks up on any noob moves that I'm completely missing. NOTE: the database write completes so I'm assuming that the HTTP Post request isn't firing, is that a safe assumption? Or JS is a different beast?

exports.stripeConnect = functions.https.onRequest((req, res) => {
    var code = req.query.code;
    const ref = admin.database().ref(`/stripe_advisors/testing`);
    var dataString = `client_secret=sk_test_example&code=${code}&grant_type=authorization_code`;
    var options = {
            url: 'https://connect.stripe.com/oauth/token',
            method: 'POST',
            body: dataString
    };

    function callback(error, response, body) {
            if (!error && response.statusCode === 200) {
            console.log(body);
            }
    }

    request(options, callback);
    return ref.update({ code: code });
});

推荐答案

我了解您要发布到request库> https://connect.stripe.com/oauth/token ,成功后,您要将code值写入数据库.

I understand that you want to POST to https://connect.stripe.com/oauth/token by using the request library and, on success, you want to write the code value to the database.

您应该在Cloud Function中使用Promise处理异步任务.默认情况下,请求不返回承诺,因此您需要使用接口包装进行请求,例如 request-promise

You should use promises, in your Cloud Function, to handle asynchronous tasks. By default request does not return promises, so you need to use an interface wrapper for request, like request-promise

因此,通常应使用以下技巧:

Therefore, the following should normally do the trick:

.....
var rp = require('request-promise');
.....

exports.stripeConnect = functions.https.onRequest((req, res) => {
    var code = req.query.code;
    const ref = admin.database().ref('/stripe_advisors/testing');
    var dataString = `client_secret=sk_test_example&code=${code}&grant_type=authorization_code`;
    var options = {
            url: 'https://connect.stripe.com/oauth/token',
            method: 'POST',
            body: dataString
    };

    rp(options)
    .then(parsedBody => {
        return ref.update({ code: code });
    .then(() => {
        res.send('Success');
    })
    .catch(err => {
        console.log(err);
        res.status(500).send(err);
    });

});

这篇关于使用请求库的Firebase函数未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆