Hi,
you are logging promise 1 after generating these. In this state they are
not fullfilled. So this log should happen:
createLog('debug', __dirname, __filename.slice(__dirname.length + 1,
-3), null, 'uci', 'promises 2', values);
Your code is a little bit hard to read. Try to use a constant for:
__filename.slice(__dirname.length + 1, -3)
A better solution is a log function like this
const log = (dir, file) => {
const file_replaced = __filename.slice(__dirname.length + 1, -3);
return (type, ...args) => {
args.unshift(dir, file_replaced)
console.log(type, ...args)
}
}
You init this log in the first line like this:
const createLog = log(__dirname, __filename);
and then use it like:
createLog('info', null, 'uci', 'apply data', dataApply);
I will also mention that your two promises did not return any data. So
after resolving these, they will be have an undefined value. So this line:
createLog('debug', __dirname, __filename.slice(__dirname.length + 1,
-3), null, 'uci', 'promises 2', values);
should return an array with undefined as values.
Hope it will help a little bit.
Am 07.06.2017 um 17:33 schrieb Catia Matos:
> 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) {
> 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) {
> 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);
> }).catch(function (err) {
> createLog('error', __dirname, __filename.slice(__dirname.length +
> 1, -3), null, 'uci', 'error promise all', err);
> });
> //the function is going to return an array like [dataCommit,
> applyCommit] or [undefined, undefined] depend of the commit / apply
> }).catch(function (e) {
> createLog('error', __dirname, __filename.slice(__dirname.length + 1,
> -3), null, 'uci', 'error', e);
> });
> }
>
> --
> 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]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/fc66d45d-8242-48bd-8904-9a1ad705f028%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/fc66d45d-8242-48bd-8904-9a1ad705f028%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
--
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/de86cdf1-160b-b9a4-417d-7e40171c7b5b%40gmx.de.
For more options, visit https://groups.google.com/d/optout.