2017-07-18 8 views
0

他のファイルでvar値を使用しようとしていますが、コードの先頭に定義すれば "プロパティ 'stats' of nullを読み取れません"というエラーが発生します。グローバルを使用していますが、それは助けにはならず、常に他のファイルではnullになります。値がコントローラ間で未定義になるnodejs

listaLive、listAreepatedおよびlistaPasserByの値を処理し、db(AgregateController)に挿入するためにこのデータを送信する別のファイルに値を取得する必要があります。私のコードがある時点で

ProbingController.prototype.pollingAllDevices = function() { 
    createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'probing', 'inicio'); 
    var numDevices = 1; 
    return Database.Devices.getDevices().then(function (devices) { 
     return new Promise(function (resolve, reject) { 
      async.each(devices, function (device, callback) { 
       logger.info('WSController-Service', 'probing', 'getRadioInfo --> ', device.id, ' :: ', numDevices++, '/', devices.length); 
       //stats ubus call wsniffer stats pie chart 
       //list ubus call wsniffer list 
       //listex ubus call wsniffer listex 

        var obj = { 
         "id": uuid.v4(), 
         // "timestamp": new Date().getTime(), 
         "device_id": device.id, 
         "data":{ 
          "stats":{ 
          }, 
          "list":{ 
          } 
         } 
        }; 

        var allPromises=[];     

        allPromises.push(Controllers.Gps.getRadioInfo('wsniffer', 'stats', {}, device)); 
        allPromises.push(Controllers.Gps.getRadioInfo('wsniffer', 'list', {}, device)); 

        Promise.all(allPromises).then(function(values){ 
         console.log("probing values -> "+device.id+" "+ JSON.stringify(values));  
         for(var j=0;j<values.length;j++){ 
          if(values[j]!=null){ 
           console.log("stats "+device.id+" --> "+ values[0]["stats"]); 
           console.log("list "+device.id+" --> "+ values[1]["list"]); 
           if(values.stats["stats"]!=undefined){ 
            obj.data.stats = values[0]["stats"]; 
           } 
           if(values.stats["list"]!=undefined){ 
            obj.data.list = values[1]["list"]; 
           }        
          } 
         } 
         var countRepetidos=0; 
         if(obj["list"]!=undefined && obj["list"].length>0){      

          var aux = obj["list"]; 
          for(var j=1;j<aux.length;j++){ 
           var tmp = aux; 
           for(var k=1;k<tmp.length;k++){ 
            if(j!=k){ 
             if(aux[j] === tmp[k]){ 
              countRepetidos++; 
              aux.splice(k,1); 
             } 
            } 
           } 
          } 
         } 

         var dataLive = []; 
         var dataRepeated= []; 
         var dataPasserBy = []; 

         var now = Date.now(); 

         var listaLive=[], listaRepeated=[], listaPasserBy=[]; 

         if(obj.data.stats!=undefined){ 
          dataLive = [obj.data.stats.since, obj.data.stats.live]; 
          dataPasserBy = [obj.data.stats.since, obj.data.stats.passerby]; 
         } 

         dataRepeated = [obj.data.stats.since, countRepetidos]; 


         createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'probing', 'obj', obj); 

         listaLive.push(dataLive); 
         listaRepeated.push(dataRepeated); 
         listaPasserBy.push(dataPasserBy); 

         Controllers.Agregate.agregateData("probing_live", 5 * 60, listaLive, dbHistConnectionString, device.id, device.network_id, device.organization_id, ["time","clients"]); 
         Controllers.Agregate.agregateData("probing_repeated", 5 * 60, listaRepeated, dbHistConnectionString, device.id, device.network_id, device.organization_id, ["time","clients"]); 
         Controllers.Agregate.agregateData("probing_passerBy", 5 * 60, listaPasserBy, dbHistConnectionString, device.id, device.network_id, device.organization_id, ["time","clients"]); 
         Database.Probing.addRow(obj); 

         global.listaLive = listaLive; 
         global.listaRepeated = listaRepeated 
         global.listaPasserBy = listaPasserBy 


        }).catch(function (e) { // error handling 
         console.log("probing - promiseall ",device.id,e); 
         if(values==null){ 
          console.log('probing values null ---->', device.id); 
         } 
         createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'probing', 'no promise all',e); 
         return callback(e); 
        }); 


      }, function (error) { 
       if (error) { 
        createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'probing', 'stops polling?!', error); 
        return reject(error); 
       } 
       createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'probing','All Devices Processed'); 
       return resolve(); 
      }); 
     }).catch(function (e) { 
      console.log("probing 1--> ",e); 
      createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'probing','promise',e); 
     }); 
    }).catch(function (e) { 
     console.log("probing 2--> ",e); 
     createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'probing','getDevices',e); 
    }); 

} 

ログ: "タイムスタンプ" と "合計" はlistaLive、listaRepeatedとlistaPasserByの内側agregate上の値であり

3|wscontro | probing stats { since: 1500454284751, 
3|wscontro | probing list [ '00:05:ca:e4:85:39', 
3|wscontro | probing dataStatsSince --> 1500454284751 
3|wscontro | probing dataStatsLive --> 236 
3|wscontro | probing dataStatsPasserBy --> 134 
3|wscontro | probing listaLive --> [ [ 1500454284751, 236 ] ] 
3|wscontro | probing listaRepeated --> [ [ 1500454284751, 0 ] ] 
3|wscontro | probing listaPasserBy --> [ [ 1500454284751, 134 ] ] 



3|wscontro | [2017-07-18 15:07:42.834] - debug: /opt/wscontroller/wscontroller-service/scripts/agregate AgregateController b2aaba26-dea0-4b2e-9b6e-6fe91c566ee9 recursviva data {"id":"c0a2941f-e617-4c14-95de-4506565b28d1","agregation_period":300,"medium":[0],"maximum":[0],"minimum":[0],"timestamp":[null,0],"sum":[null],"type":"probing_repeated","device_id":"b2aaba26-dea0-4b2e-9b6e-6fe91c566ee9","network_id":"7e709c63-45c5-4716-b596-ece3468b25f9","organization_id":"ce4b69af-bdce-4f1b-ba71-dd03544205d5","labels":["time","clients"]} 

3|wscontro | probing timestamp [ undefined, 0 ] 
3|wscontro | probing sum undefined 

私はあなたがexportsオブジェクトを使用したい任意の値を共有することができ

+0

ここで、変数を別のファイルで使用しようとしていますか?どのラインがあなたにそのエラーを与えていますか?これがNode.jsの場合は、グローバル変数を使用しないでください。モジュールを使用する。 –

+0

私は//obj["data"].stats=statsData["stats "]というコメント行を使用してデータを使用していましたが、指定されたエラーは発生しましたが、空のエラーを発生させる...私はAgregateControllerのこのデータを理解する必要があります..しかし、このファイルで渡されたデータはnullです... – ricardol

答えて

0

... agregator上のリストのこの値を必要とします。

エクスポートを使用してvar変数をエクスポートしようとします。例:

**helper.js** 

const hello = 'hello world' 
exports.hello = hello 

**index.js** 

const example = require('./helper.js').hello 
console.log(example) //Will show 'hello world' 
+0

私は理解しています。何の方法もありませんか? :\ http://plnkr.co/edit/telrz9HAdVEwq18pJ5js?p=ここに私のコードをプレビュー – ricardol

+0

私はあなたの質問を誤解したと思います。あなたが解決策を使って約束を使用している場合、あなたは必要なデータを渡すことができます。お約束を他のファイルで呼び出すと、確実に '' 'then'''メソッドがあなたの変数に従います。ここでは、これを手助けできるドキュメントがあります。https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve – LuisPinto

+0

私はコード解決(listaLive)で何をしようとしましたか他のファイルにうんざり...どうすればいいですか? http://plnkr.co/edit/telrz9HAdVEwq18pJ5js?p=previewマイコードが更新されました – ricardol

関連する問題