2017-02-11 149 views
0

這是我的代碼。當我發送請求時,我沒有收到結果。我不明白爲什麼它不是在情況1工作。我修改它一點點,並把它放在然後,它正在工作。節點JS沒有返回結果

我在做什麼錯在這裏? 爲什麼它不適用於案例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()只是b在最後}。

回答

1

在案例1中調用then直接不會等到您提供給它的函數中的所有代碼都已完成。因此,在填充有效載荷之前,呼叫producer.send(...)太早。正確的方法是將producer.send(...)包含到您給then()的功能中。撥打res.end()不應該在這裏相關。

+0

感謝您的解釋。但如果你看到https://github.com/Gowtham95india/CapVengine/blob/master/server.js statsCollector函數,我打電話producer.send工作正常。是否因爲forEach循環延遲了調用並且Object被推送? 你說過,producer.send啓動得太早,如果是這樣的話,它應該返回200.不是嗎?因爲我在producer.send函數中返回200,即使我發送空有效負載也沒關係,kafka返回true。 你能解釋一下嗎? –

+0

'statsCollector()'的實現是非常脆弱的,並且不能保證你的'payloads'結構在你把它交給'producer.send()'前完全填充。 – Marc

+0

您是否理解Javascript中的Promises背後的概念?他們「活在」then()函數等...... – Marc