来自promise的返回值是未定义的Node.Js

时间:2017-06-06 09:40:45

标签: javascript node.js postgresql openwrt

我正在使用ubus与物理设备进行通信,我尝试设置配置,然后使用该结果提交设备内的文件(写入)但我始终从uci中获取未定义请求但在功能内部我得到ubus代码[0]。

ServicesController.prototype.updateWifi = function (req, res, next) {
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'begin');
    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){     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) {
                if (dataApply && dataApply.hasOwnProperty('result') && data.result[0] == 0) {
                  createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
                  return dataApply.result[1];
                }
            })
          promises.push(p2);
        }
    }
    return Promise.all(promises);
}

我的问题是这个函数,因为它没有返回任何值,但我得到了ubus状态代码[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);
    var res;
  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) {
      return 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);
        if (data.result) {
          if (data.result[0] == 0) {
              res = data.result[1];
            return res;
          }
        }
        throw new Error("no data");
      });
     createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'dataResuult', res);
    }

    throw new Error("no dataAuth");
  }).catch(function(e) {
    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', e);
    throw e;
  });
}

当我在此日志console.log('updateWifi ---> data',wifiData)中的updateWifi tha数据中打印时,它返回undefined。无论如何从函数ServicesController.prototype.uci返回值?

此处的所有代码:http://plnkr.co/edit/5QaSxPXkz9Ts0mzCY4RE?p=preview

0 个答案:

没有答案