【发布时间】:2018-08-25 07:18:55
【问题描述】:
我正在编写一个 Firebase 云函数来攻击 API,获取一些数据,然后将其序列化,然后将数据上传到我的 Firestore 数据库。这是使用 Cron 安排的,每天执行。我的功能工作正常,一切正常。但是我的http请求调用了两次success函数。
我正在使用request 来管理 HTTP 请求。我的代码如下:
return request({
uri: uri,
method: "GET",
qs: propertiesObject,
json: true
}, function (error, response, body) {
if (response.statusCode == 200) {
return serializeResponse(body, region, bracket, firebaseResponse)
} else {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
return firebaseResponse.status(400).send("The petition to Battle.NET API has failed");
}
});
serializeResponse 方法只是将给定的数据序列化并上传到 Firestore,仅此而已。 FirebaseResponse 是从 http 请求到云函数的原始响应,需要关闭发送成功或错误代码以避免执行无限循环。
但是,问题是function 被调用了两次,我不知道为什么。在我向代码添加愚蠢的布尔控件或丑陋的东西之前,我更愿意寻求帮助。
【问题讨论】:
标签: node.js firebase http request google-cloud-functions