节点JS不返回结果

时间:2017-02-11 19:44:54

标签: javascript node.js api request

这是我的代码。当我发送请求时,我没有收到结果。我不明白为什么它不能在案例1中工作。我稍微修改它并把它放在然后里面,它正在工作。

我在这里做错了什么? 为什么它不适用于案例1? 如何在案例1中使用res.end(),例如案例3,使其工作。

案例1:

var vigeonCollector = function(req, res) {

// console.log(req.body);
try {
    var store = JSON.parse(JSON.stringify(req.body).toString('utf8').replace("'",'"'));
}
catch (e) {
    console.log("Error in JSON Parsing!");
    return res.status(422).json({"status":false, "message":"Unparsble JSON"});
}

var payloads = [];

store.timestamp = getTimeStamp(); // Should be tagged with current timestamp.

// Adding event_day IST and UTC format.
var currentUTCTime = new Date();
var currentISTTime = new Date(currentUTCTime.toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }));
store.event_day = currentUTCTime.toLocaleString().split(',')[0];
store.event_day_ist = currentISTTime.toLocaleString().split(',')[0];
store.advertiser_id_met = store.advertiser_id;
store.device_id_met = store.device_id;


// Call get on redis only once and use it for attribution.
var redis_result;
redis.get(store.device_id).then(function(jresult){
    redis_result = JSON.parse(jresult);
    console.log(redis_result);
    if (store.event_type == "UNINSTALLS") {

        // Attributing user installed UTM Params.
        store.event_properties.utm_medium = redis_result.user_installed_medium;
        store.event_properties.utm_source = redis_result.user_installed_source;
        store.event_properties.utm_campaign = redis_result.user_installed_campaign;
        store.advertiser_id = redis_result.advertiser_id;

    }
    else {

        // Tagging last user session UTM Params.
        store.event_properties.utm_medium = redis_result.medium;
        store.event_properties.utm_source = redis_result.source;
        store.event_properties.utm_campaign = redis_result.campaign;

    }

    temp_obj = { topic: "vnk-clst", messages: JSON.stringify(store)};
    payloads.push(temp_obj);
    console.log(payloads);
    console.log("I'm Done!");

});

producer.send(payloads, function(err, data){
    console.log(data);
    if (err) return res.status(503).json({ "status": false, "message": "503 Service Unavailable", "error": err });
    else return res.status(200).json({ "status": true, "message": "OK"});
});

}

案例2:

var vigeonCollector = function(req, res) {

// console.log(req.body);
try {
    var store = JSON.parse(JSON.stringify(req.body).toString('utf8').replace("'",'"'));
}
catch (e) {
    console.log("Error in JSON Parsing!");
    return res.status(422).json({"status":false, "message":"Unparsble JSON"});
}

var payloads = [];

store.timestamp = getTimeStamp(); // Should be tagged with current timestamp.

// Adding event_day IST and UTC format.
var currentUTCTime = new Date();
var currentISTTime = new Date(currentUTCTime.toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }));
store.event_day = currentUTCTime.toLocaleString().split(',')[0];
store.event_day_ist = currentISTTime.toLocaleString().split(',')[0];
store.advertiser_id_met = store.advertiser_id;
store.device_id_met = store.device_id;


// Call get on redis only once and use it for attribution.
var redis_result;
redis.get(store.device_id).then(function(jresult){
    redis_result = JSON.parse(jresult);
    console.log(redis_result);
    if (store.event_type == "UNINSTALLS") {

        // Attributing user installed UTM Params.
        store.event_properties.utm_medium = redis_result.user_installed_medium;
        store.event_properties.utm_source = redis_result.user_installed_source;
        store.event_properties.utm_campaign = redis_result.user_installed_campaign;
        store.advertiser_id = redis_result.advertiser_id;

    }
    else {

        // Tagging last user session UTM Params.
        store.event_properties.utm_medium = redis_result.medium;
        store.event_properties.utm_source = redis_result.source;
        store.event_properties.utm_campaign = redis_result.campaign;

    }

    temp_obj = { topic: "vnk-clst", messages: JSON.stringify(store)};
    payloads.push(temp_obj);
    console.log(payloads);
    console.log("I'm Done!");

producer.send(payloads, function(err, data){
    console.log(data);
    if (err) return res.status(503).json({ "status": false, "message": "503 Service Unavailable", "error": err });
    else return res.status(200).json({ "status": true, "message": "OK"});
});

});

}

案例3:在最后一次之前添加res.end()。

1 个答案:

答案 0 :(得分:1)

在案例1中对then的调用不会等到你给它的函数中的所有代码都完成之后。因此,在填写有效负载之前,对producer.send(...)的调用过早启动。正确的方法是将producer.send(...)包含在您为then()提供的功能中。拨打res.end()的电话不应该与此相关。