0

I am trying to export a variable from app.js to export.js file but for some reasons it does not work. It used to work but no longer I am able to get the value of jsondata variable. Please help me!

//route.js

var express = require('express');
var js_app = require('./app.js');
//console.log(js_app);
var data = js_app;
//console.log(data);
module.exports = function(){                
    var app = express();
    console.log("Log inforLog infor..");
    app.set('views', './static/');
    app.set('view engine', 'jade');
    app.get('/', function(req, res) {
      res.render('index', data);
    });            
    return app;

};

//app.js

var request = require('request');
var setting = require('./config/configuration.js');

request({url: setting[0]},function(error, response, body){
    if(!error && response.statusCode == 200){
            console.log(JSON.parse(response.body));
    } 
    });


request({url: setting[0]+'/'+"dr_sc_values_from_31415927/_design/id_view/_view/viewID?limit=10"},function(error, response, body){
    console.log(response.statusCode);
    if(!error && response.statusCode == 200){
     var response_view = JSON.stringify(eval("(" + response.body + ")"));
     var jsondata = JSON.parse(response_view).rows;
      console.log(jsondata);
      module.exports = jsondata; // this is one, I want to exprt
    } 
    });
  • 1
    Setting `module.exports` in an async callback will almost never work. The problem is that the caller (the code that is supposed to be examining the exported data) has NO idea when that data is actually set. Usually, you will be looking at it and trying to use it BEFORE it has been set. – jfriend00 Feb 16 '16 at 23:04
  • 1
    This can never have worked. At the moment you are importing `app.js`, `module.exports = jsondata;` isn't set yet. I recommend to read http://stackoverflow.com/q/14220321/218196 and http://stackoverflow.com/q/23667086/218196 . – Felix Kling Feb 16 '16 at 23:04

0 Answers0