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?