Hi, see inline comments.
2017-06-07 17:33 GMT+02:00 Catia Matos <[email protected]>: > I have the Ubus.ucirequest function and Im returning a promise and is > resolving the value but in the end when i log the value i get promise > {"isFulfilled":false,"isRejected":false} > > Because of this the rest of my code is not working... In my uci function > when I try to resolve another two promises and push them into an array when > I iterate over the values the array is empty {} but before I do > Promise.all(promises) when I log the value of promises I get > > 2|wscontro | [2017-06-07 15:11:06.610] - debug: > /opt/wscontroller/wscontroller-api/routes/services ServicesController NA uci > promises 1 > [{"isFulfilled":false,"isRejected":false},{"isFulfilled":false,"isRejected":false}] > > 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); > > var promise = new Promise(function (resolve, reject) { > 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) { > createLog('info', __dirname, > __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'promise > resolved'); > resolve(data.result[0]); > } > } else { > reject("no data"); > } > }); > } else { > reject("no data auth"); > } > }).catch(function (e) { > createLog('error', __dirname, __filename.slice(__dirname.length + > 1, -3), device.id, 'uciRequest', e); > reject(e); > }); > }); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uciRequest', 'promise', promise); > return promise; > } > ServicesController.prototype.uci = function(device, config, path, section, > property, value, apply, commit){ > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'inicio'); > > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'config', config); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'path', path); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'section', section); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'option', property); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'value', value); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'apply', apply); > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'commit', commit); > > var values = {}; > values[property] = value; > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), > device.id, 'uci', 'values', values); > > return Controllers.Ubus.uciRequest('set', {"config": config, "section": > section, values}, device) > .then(function (uciData) { > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, > -3), device.id, 'uci', 'uciData',uciData ); > var promises = []; > > if (uciData!=null) { > createLog('info', __dirname, __filename.slice(__dirname.length + > 1, -3), device.id, 'uci', 'depois do if do uciData' ); > > if (commit){ > createLog('debug', __dirname, > __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit'); > var p1 = Controllers.Ubus.uciRequest('commit', {"config": > config}, device) > .then(function (dataCommit) { > > this function doesn't return a promise. > if (dataCommit && dataCommit.hasOwnProperty('result') && > data.result[0] == 0) { > createLog('info', __dirname, > __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', > dataCommit); > } > }) > promises.push(p1); > } > > if(apply){ > createLog('debug', __dirname, > __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply'); > var p2 = Controllers.Ubus.fileExec(device.id, "exec", path, > "restart") > .then(function (dataApply) { > > this neither. > if (dataApply && dataApply.hasOwnProperty('result') && > data.result[0] == 0) { > createLog('info', __dirname, > __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', > dataApply); > } > }) > promises.push(p2); > } > } > > createLog('debug', __dirname, __filename.slice(__dirname.length + 1, > -3), null, 'uci', 'promises 1', promises); > return Promise.all(promises).then(function(values){ > createLog('debug', __dirname, __filename.slice(__dirname.length + > 1, -3), null, 'uci', 'promises 2', values); > > so your values here should be an array with 0 - 2 undefined elements. -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CAA%3DcwqGhX0LkTP9qrMnh_VMsgrm47OuwHnunQw%3D%3DLubS7rhX8A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
