i'm having trouble filling my var sendData and work with promises because in my last post (variable value becomes undefined in NodeJS after if cycle) I tried to have this value fill with info but get always undefined caused i have two resolves...
I cant added the map info from db into sendData, that's the point of doing this first resolve promise. So here's my code can anyone help me to understand how to change this so i can have my sendData fill with info and then send it in a post Request?
GpsController.prototype.sendDeviceInfo = function (gps, device) {
var sendData = {};
var postOptions = {
host: 'cdrburner.appspot.com',
port: 443,
path: '/api/v1/cdr/carris/cfc04b9a-e952-4eb4-9072-618d3d0232de/analitycs/json',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
return new Promise(function(resolve, reject) {
sendData.gps = null;
if (gps != null) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','gps',gps);
sendData.gps = gps;
}
sendData.device = {
"id": device.id,
"sn": device.data.sn,
"name": device.data.name,
"organization_id": device.organization_id,
"network_id": device.network_id
};
Database.Services.getServicesMapInfoById(device.id).then(function(mapData){
if(mapData!=undefined){
var map = mapData.map;
console.log("sendDeviceInfo getServicesMapInfoById map >>>>>>>>>>>>>>>>>>>>> ", map);
sendData.map=map;
resolve(sendData);
} else {
resolve(null);
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','FATAL',e);
return reject(e);
});
sendData.wireless = {deviceClients: device.data.clients};
sendData.modems = device.data.modems;
if (device.network_id != null) {
sendData.device.network_name = device.network_data.name;
}
console.log("sendDeviceInfo map --------------------------------->", sendData.map);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','sendData ', sendData);
var postCallback = function (postRes) {
postRes.on('data', function (data) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','Post GPS data');
});
postRes.on('end', function () {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','Post GPS end');
return resolve();
});
postRes.on('error', function (postErr) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','Post Error: ', postErr);
return reject(postErr);
});
}
var postReq = https.request(postOptions, postCallback);
postReq.write(JSON.stringify(sendData));
postReq.setTimeout(5000, function () {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo', 'Request Timeout');
postReq.abort();
});
postReq.on('error', function (postErr) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo', 'Post Error: ' + postErr);
return reject(postErr);
});
postReq.end();
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','FATAL',e);
return reject(e);
});
}
via Catia Matos
No comments:
Post a Comment