Reputation: 638
Here is my code. When I send the request, I'm not getting the result back. I didn't understand why it is not working in case 1. I modified it a little bit and put that inside then, and it is working.
What I'm doing wrong here? Why it is not working in Case 1? How to make it work in case 1 with out using res.end() like case 3.
Case 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"});
});
}
Case 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"});
});
});
}
Case 3: Adding res.end() just before the last }.
Upvotes: 0
Views: 156
Reputation: 1976
The call to then
in case 1 directly does not wait until all of your code within the function you gave to it has finished. So the call to producer.send(...)
starts too early before you filled your payload. The right way is to include the producer.send(...)
into the function you gave to then()
. The call to res.end()
shouldn't be relevant here.
Upvotes: 1