我正在编写一个攻击 API 的 Firebase 云函数,获取一些数据并在序列化后将数据上传到我的 Firestore 数据库。这是使用 Cron 安排的,每天执行一次。我的功能工作正常,一切正常。但是我的 http 请求调用了成功函数两次。
我正在使用 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
被调用了两次,我不明白为什么。在我向代码添加愚蠢的 bool 控件或丑陋的东西之前,我宁愿寻求帮助。
最佳答案
因为每次请求一个页面浏览器默认是favicon.ico,做个判断
var pathname = url.parse(request.url).pathname;
if(pathname != '/favicon.ico'){
}
关于node.js - 请求:使用请求 node.js 两次调用响应函数获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358118/