快到了,但由于某种原因,我的 HTTP post 请求没有触发,最终函数超时。我完全疯了,并发布我的代码,看看是否有人注意到我完全错过的任何菜鸟 Action 。注意:数据库写入完成,因此我假设 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 });
});
最佳答案
我了解到您想发布到 https://connect.stripe.com/oauth/token通过使用 request
库,成功后,您希望将 code
值写入数据库。
您应该在云函数中使用 Promise 来处理异步任务。默认情况下,请求不会返回 Promise,因此您需要使用接口(interface)包装器来请求,例如 request-promise
因此,通常应该执行以下操作:
.....
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);
});
});
关于javascript - 使用请求库的 Firebase 函数未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525371/