0

I am taking first steps with Node.JS and came across a weird issue i cant figure out. I have this events object from the upcoming_events.js file i am exporting like this:

 module.exports = function(passport, FacebookStrategy, config, mongoose) {    
        var fbEvent = new mongoose.Schema({
            eventID: String,
            eventName: String,
            eventPic: String,
            rsvp_list: Array,
            category: String
        })

        var eventModel = mongoose.model('fbEvent', fbEvent);

        eventModel.find({}, function(err, events) {
            if (err) throw err;

            module.exports.events = events;
        })

    }

and in the receiving side, the file 'routes.js' i was able to access the data like this:

var events = require('../data/upcoming_events.js');

router.get('/upcoming_events', securePages, function(req, res, next){
    console.log(events.events[0].eventName);//accessing data....
        res.render('upcoming_events', {title: 'Welcome to aDating - Upcoming Events'});
})

and the log i would get looks like this:

{ _id: 55aa582edeea927016d5af1c,
  eventID: '1111',
  eventName: 'Breakfast Club',
  eventPic: '4d669b',
  category: 'Party',
  __v: 0,
  rsvp_list:
   [ '12343242'
 ] }

so far everything works fine but now i want to pass this data to another file ,'proccess_data.js' and than send it to routes.js. The proccess looks like this (no functionality for demonstration):

var events = require('../data/upcoming_events.js');
module.exports.user = events;

but now when i require the data from the proccess_data.js file and trying to access it in the same way:

var data= require('../data/proccess_data.js');

router.get('/upcoming_events', securePages, function(req, res, next){
    console.log(data.events[0].eventName);//accessing data....
        res.render('upcoming_events', {title: 'Welcome to aDating - Upcoming Events'});
})

i get the following error in the console:

TypeError: Cannot read property '0' of undefined

one more detail, when i export events from proccess_data.js like this:

module.exports = function(passport, FacebookStrategy, config, mongoose) {
    var events = require('../data/upcoming_events.js');
    module.exports.user = events;
}

and access it like this in routes.js like this:

console.log(data.user);

this is what prints out:

{ [Function]
  events:
   [ { _id: 55aa582edeea927016d5af1c,
       eventID: '1111',
       eventName: 'TONIGHT - Andreas Henneberg // Breakfast Club',
       eventPic: '69b',
       category: 'Party',
       __v: 0,
       rsvp_list:
        [ '12343242',
          '12366662' ] },
     { _id: 55aa6a751178e94823016053,
       eventID: '2222', ...And so on...

any idea why is that please?

Donoven Rally
  • 1,670
  • 2
  • 17
  • 34
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Blakes Seven Jul 19 '15 at 02:42

2 Answers2

0

1) Export from upcoming_events.js - is function;

2) In module proccess_data.js - you export it as user function;

3) And in routes.js it's coming as object where key user is function.

So, try data.user()[0].eventName.

stdob--
  • 28,222
  • 5
  • 58
  • 73
0

You're using the NodeJS environment a little oddly.

Firstly, You should use module.exports to export the main class of the JS file. You definitely don't (and can't) use it in a runtime (anything other than an immediately-invoked function expression) sort of way, i.e.

eventModel.find({}, function(err, events) {
    if (err) throw err;
    module.exports.events = events;
})

Secondly, all the modules that your file needs should be required at the top of your JS file and, again, not in a runtime sort of way: i.e.

module.exports = function(passport, FacebookStrategy, config, mongoose) {
    var events = require('../data/upcoming_events.js');
    module.exports.user = events;
}

The second item has more caveats than the first one, but it's a good practice to start with.

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100