从2承诺的返回值未定义

时间:2017-06-06 15:17:03

标签: node.js

我遇到了一个函数(uci)的麻烦,期望返回一个承诺,但我同时返回两个,当我记录它时,我得到了

uci promise Promise {
2|wscontro |   _bitField: 0,
2|wscontro |   _fulfillmentHandler0: undefined,
2|wscontro |   _rejectionHandler0: undefined,
2|wscontro |   _promise0: undefined,
2|wscontro |   _receiver0: undefined }

我需要将此值传递给函数并处理uciData继续...

 ServicesController.prototype.updateWifi = function (req, res, next) { //this is what i call when i click on the button in the screen
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'inicio');
    var data = req.body;
    var state;
    if(data.state=="1"){
        state=1;
    } else if(data.state=="0"){
        state=0;
    }
    if (isEmptyObject(data)) {
        res.status(400).send({error: errorMessage.emptyBody});
        return;
    }

    if (data.sn === undefined || data.sn === "") {
        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'Invalid serial number');
        res.status(400).send({error: "Invalid serial number"});
        return;
    }

     Database.Devices.getDeviceBySn(data.sn).then(function (device) {
        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'device', device);
        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'device.id', device[0].id);

        var ifname=data.ifname;
        return ServicesController.prototype.uci(device[0], "teste_wireless", "wifi", ifname, "disabled", data.state, true, true).then(function(wifiData){
            console.log('updateWifi ---> data',wifiData);
            if (wifiData != undefined) {
                res.status(404).end();
            } else {
                console.log('updateDeviceInterface ', device[0].id, ' for ', ifname, ' to ', state);
                Database.Services.updateWifiDeviceInterface(device[0].id, ifname, state);
                res.status(200).send(wifiData);
            }
        }).catch(function (e) {
            createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', e);
            res.status(500).send(e);
        });
    });
}       

ServicesController.prototype.uci = function(device, config, path, section, opt, value, apply, commit){
    createLog('info', __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', opt);
    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={opt:value};

    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'session', radioAccess[device.id]);

    var result;

    Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device).then(function (uciData) {
        console.log('uci uciData -->', uciData); //this gets undefined

        if (uciData!=null) {
            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do if do uciData' );
            /*return new Promise(function (resolve, reject) {
                return Controllers.Ubus.uciRequest('changes', config, device).then(function (dataAuth) {
                    if (dataAuth!=null) {
                        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'Invalid serial number');
                        res.status(400).send({error: "Cant apply changes"});
                        return;
                    }
                }).catch(function (e) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
                    reject(e);
                });
            }).catch(function (e) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
                    reject(e);
                });
        };*/
            if(commit){
                createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit');
                return new Promise(function (resolve, reject) {
                    Controllers.Ubus.uciRequest('commit', {"config": config}, device).then(function (dataCommit) {
                        if (dataCommit) {
                            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', dataCommit);
                            if (dataCommit.result) {
                                if (dataCommit.result[0] == 0) {
                                    result = dataCommit.result[1];
                                } else {
                                    resolve(null);
                                }
                            } else {
                                resolve(null);
                            }
                        }
                    }).catch(function (e) {
                        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
                        reject(e);
                    });
                }).catch(function (e) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
                    reject(e);
                });
            }
            if(apply){
                createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
                return new Promise(function (resolve, reject) {
                    Controllers.Ubus.fileExec(device.id, "exec", path, "restart").then(function (dataApply) {
                        if(dataApply){
                            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
                            if (dataApply.result) {
                                if (dataApply.result[0] == 0) {
                                    result+= dataApply.result[1];
                                } else {
                                    resolve(null);
                                }
                            } else {
                                resolve(null);
                            }
                        }
                    }).catch(function (e) {
                        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
                        reject(e);
                    });
                }).catch(function (e) {
                    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
                    reject(e);
                });
            }
        }
        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do fim do if do uciData' );
    }).catch(function (e) {
        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
    });
}

1 个答案:

答案 0 :(得分:0)

此代码需要maaaaaasssive重构 但是,您可以使用Promise.all在2个承诺完成后返回。

看起来像这样。

ServicesController.prototype.uci = function(device, config, path, section, opt, value, apply, commit){
    var values={opt:value};

    return Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device)
      .then(function (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('info', __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);
                      return dataCommit.result[1];
                    }
                  })
              promises.push(p1);
            }

            if(apply){
                createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
                var p2 = Controllers.Ubus.fileExec(device.id, "exec", path, "restart")
                  .then(function (dataApply) {
                  ...
                })
              promises.push(p2);
            }
        }
        return Promise.all(promises);
        //the function is going to return an array like [dataCommit, applyCommit] or [undefined, undefined] depend of the commit / apply
    })
}