Promise doenst return value and got undefined

cmatos
cmatos edited June 2017 in General
I'm using ubus to communicate with physical devides and I'm trying to set a configuration and then use that result to commit to a file (write) inside the device but i always get undefined from the uci request but inside the function i get ubus code [0].

UbusController.prototype.uciRequest = function (procedure, signature, device) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'inicio');
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'device id', device.id);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'procedure', procedure);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'signature', signature);

return new Promise(function (resolve, reject) {
return Controllers.Ubus.getSession(device.id).then(function (dataAuth) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'dataAuth', dataAuth);

if (dataAuth) {
Controllers.Ubus.execCommand(device.id, "uci", procedure, signature).then(function (data) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'data', data);
var res=data;
if (data.result) {
if (data.result[0] == 0) {
return resolve(data.result[1]);
} else {
return resolve(null);
}
} else {
return resolve(null);
}
});
}
return (res);
resolve();
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', e);
reject(e);
});

createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'fim');
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', e);
reject(e);
});
}

My code is triggered when i click in a button on my browser which call updateModem function of Services Controller (http://plnkr.co/edit/5QaSxPXkz9Ts0mzCY4RE?p=preview)

To more info see: https://stackoverflow.com/questions/44386538/return-value-from-promise-is-undefined-node-js
Tagged:
Sign In or Register to comment.