Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用请求库的Firebase函数未触发_Javascript_Firebase_Request_Google Cloud Functions - Fatal编程技术网

Javascript 使用请求库的Firebase函数未触发

Javascript 使用请求库的Firebase函数未触发,javascript,firebase,request,google-cloud-functions,Javascript,Firebase,Request,Google Cloud Functions,差不多了,但由于某种原因,我的HTTP post请求没有启动,最终函数超时。我完全疯了,发我的代码,看看有没有人注意到我完全错过的任何noob动作。注意:数据库写入完成,所以我假设HTTP Post请求没有触发,这是安全的假设吗?或者JS是另一种野兽 exports.stripeConnect = functions.https.onRequest((req, res) => { var code = req.query.code; const ref = admin.da

差不多了,但由于某种原因,我的HTTP post请求没有启动,最终函数超时。我完全疯了,发我的代码,看看有没有人注意到我完全错过的任何noob动作。注意:数据库写入完成,所以我假设HTTP Post请求没有触发,这是安全的假设吗?或者JS是另一种野兽

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 });
});

我知道您想使用
请求
库发布到,如果成功,您想将
code
值写入数据库

您应该在云功能中使用承诺来处理异步任务。默认情况下,请求不返回承诺,所以您需要为请求使用接口包装器,如

因此,通常应采取以下措施:

.....
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有什么关系?看起来您正在尝试连接到stripe?您使用的http库是什么?使用
parsedBody
可以得到什么?(当承诺在then中解析时)?不要忘记安装请求承诺(
npm install--save request promise
),这样它就可以通过删除来工作。然后()=>bc它抛出一个意外的令牌错误并手动停止网站。如果我只是让网站运行,它会不断尝试并最终拒绝令牌。为了解决这个问题,将使用Firebase写入令牌作为一个指示器,将用户重定向到不同的网站。想象一下在JS中有更好的方法来实现这一点,这样所有人都可以使用这是我的建议。