Thursday, 25 May 2017

i want the strJson value to be passes to the main file that is pull_data.js

this the the main pull_data2.js file

   var https = require('https')
var fs = require ('fs')
var int = 0;
var id=[];
var i=0;
var Q = require('q');
var Sync = require('sync');
var cd = require('child_process')
var program = require('./push_data.js')
var program1 = require('./pull_sensor_id2.js')
function getJSON(cb){
          Q.fcall(program1.values_of_sensor()).then(function(id){

            console.log ('out of callback');

          console.log(id)
          while(i<id.length())
          {
            console.log(id[i]);
            var options={
                host: 'www.imonnit.com',
                path: '/json/SensorRecentDataMe0ssages/xxxxxxxxxxxxx?sensorID='+id[i]+'&minutes=10&lastDataMessageGUID=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
                method: 'GET'
            };
            i++;
            https.request(options, function(res){
              var body = '';
              res.on('data',function(chunk){
                  body+= chunk;
                  console.log('body');
              });
              res.on('end', function(){
                  var result = JSON.parse(body);
                  //program.push_sensor_data(result)
                  console.log('After pushing data');
                  fs.appendFile('Input.json',JSON.stringify(result),function(err){
                      if(err){
                          return console.log('Error while writing data:' , err);
                      }
                  });
                  cb(null,result);
              });
              res.on('error',cb)
          })
          .on('error',cb)
          .end();
}
})
}
setInterval(function() { getJSON(function(err, result){
        if(err){
          return console.log('Error while getting data:' , err);
        }
        int++;
        console.log(int);
}); }, 10000);

this is another file that is pull_sensor_id2.js

var strJson = [];
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/smartpole";
//var program3 = require('./pull_data3.js')
//var mongoose = require('mongoosse')
//mongoose.connect(url)
//var db = mongoose.connection
module.exports =
{
  values_of_sensor:function(){
    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      var cursor = db.collection("sensordetails").find({},{sensorid:1,_id:0}).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        var intCount = result.length;
        if (intCount > 0) {

          for (var i = 0; i < intCount;) {
            strJson [i] = result[i].sensorid
            i = i + 1;
          }
        }
        console.log(strJson);
        return strJson;
        db.close();
      });

    });
  }
}

I want to get the value of strJson in pull_data3.js file so that I can use it in as id which will be an arrary. But it seems that I am not able to return strJson value to id please help.



via Krish Mehta

No comments:

Post a Comment