请求:使用请求node.js调用响应函数两次进行GET请求

时间:2018-07-16 09:00:14

标签: node.js firebase http request google-cloud-functions

我正在编写一个攻击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被两次调用了,我不知道为什么。在向代码中添加愚蠢的布尔控件或丑陋的代码之前,我希望寻求帮助。

1 个答案:

答案 0 :(得分:0)

由于每次请求页面时浏览器默认为favicon.ico,请做出判断

var pathname = url.parse(request.url).pathname;    
if(pathname != '/favicon.ico'){
}
相关问题