我正在编写一个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");
}
});
方法只是序列化给定的数据并将其上传到firestore,而不是别的。
serializeResponse
是http请求对云函数的原始响应,该函数需要关闭以发送成功或错误代码,以避免无限的执行循环。然而,问题是
FirebaseResponse
被调用了两次,我不知道为什么。在我给代码添加一个愚蠢的布尔控件或一些难看的东西之前,我宁愿寻求帮助。 最佳答案
因为每次请求页面时,浏览器默认为favicon.ico,所以要做出判断
var pathname = url.parse(request.url).pathname;
if(pathname != '/favicon.ico'){
}
关于node.js - 请求:使用请求node.js调用响应函数两次进行GET请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51358118/