Reputation: 5039
So this is the error message:
/home/alex/Documents/Projects/ontario-job-portal/node_modules/path-to-regexp/index.js:63 path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?')) ^
TypeError: Cannot read property 'length' of undefined at pathtoRegexp (/home/alex/Documents/Projects/ontario-job-portal/node_modules/path-to-regexp/index.js:63:49) at new Layer (/home/alex/Documents/Projects/ontario-job-portal/node_modules/express/lib/router/layer.js:45:17) at Function.use (/home/alex/Documents/Projects/ontario-job-portal/node_modules/express/lib/router/index.js:464:17) at Object. (/home/alex/Documents/Projects/ontario-job-portal/routes/event.js:11:8) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object. (/home/alex/Documents/Projects/ontario-job-portal/app.js:15:13) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12)
If I go to the place in app.js that's supposed to throw the error I see just this:
var event = require('./routes/event');
This is the correct path. If I make the path incorrect I get this:
Error: Cannot find module './routes/events'
So the path is correct.
If I comment out this piece the code runs. But unfortunately I need this import to work. Commenting out the entire file doesn't help (throws the same length of undefined error).
Reinstalling all node modules doesn't help either.
This is the code for event.js
{
var express = require('express');
var router = express.Router();
var Account = require('../models/account');
var Event = require('../models/event');
var functions = require('../globals/functions');
var GeneralInfo = require('../models/general-info');
var Posting = require('../models/posting');
}
router.use(functions.isLogisRegisteredForEventgedIn, functions.isBlocked);
/* GET main Event page. */
router.get('/', functions.isRegisteredForEvent, async (req, res, next) => {
const event = await GeneralInfo.findOne().then(r => Event.findById(r.activeEventId)).catch(e => console.log(e));
const postings = await Posting.find().where('eventId').equals(event._id).limit(10).catch(e => console.log(e));
res.render('event/', {
title: event.eventTitle,
user: req.user,
event: event,
employers: employers,
postings: postings,
});
});
// Create Event
router.route('/create')
.get(functions.isAdmin, (req, res, next) => {
res.render('event/create', {
title: 'Create a New Event',
user: req.user,
errorMsg: '',
})
})
.post(functions.isAdmin, (req, res, next) => {
var r = req.body;
console.log('status', r.status);
Event.create(new Event({
'eventTitle': r.eventTitle,
'location': r.location,
'startDate': r.startDate,
'endDate': r.endDate,
'startTime': r.startTime,
'endTime': r.endTime,
'createdBy': req.user._id, // Here we will store the _ID from the user EOSP.
})).then((event) => {
GeneralInfo.find().then(doc => {
if (doc.length > 0) {
if (req.body.status === 'true') {
GeneralInfo
.findByIdAndUpdate(doc[0]._id, {'activeEventId': event._id,})
.catch(e => console.log(e));
}
} else {
// if (req.body.status === 'true') {
GeneralInfo
.create(new GeneralInfo({'activeEventId': undefined,}))
.catch(e => console.log(e));
// }
}
}).catch(e => console.log(e));
}).then(() => res.redirect('/admin')).catch(e => console.log(e))
});
// Event Details
router.route('/details/:_id')
.get(functions.isAdmin, (req, res, next) => {
Event.findById(req.params._id).then(doc => {
res.render('event/details', {
title: 'Event Details',
user: req.user,
event: doc
})
});
})
.post(functions.isAdmin, (req, res, next) => {
var r = req.body;
Event.findByIdAndUpdate(req.params._id, {
$set: {
'eventTitle': r.eventTitle,
'location': r.location,
'startDate': r.startDate,
'endDate': r.endDate,
'startTime': r.startTime,
'endTime': r.endTime,
}
}).then(() => res.redirect('/admin')).catch(e => console.log(e));
});
// Activate the Event
router.get('/activate/:_id', functions.isAdmin, (req, res, next) => {
GeneralInfo.findOne().then(r => {
GeneralInfo.findByIdAndUpdate(r._id, {
'activeEventId': req.params._id,
}).then(() => res.redirect('/admin'))
})
});
router.get('/deactivate/:_id', functions.isAdmin, (req, res, next) => {
GeneralInfo.findOne().then(r => {
GeneralInfo.findByIdAndUpdate(r._id, {
'activeEventId': undefined,
}).then(() => res.redirect('/admin'))
})
});
router.get('/close/:_id', functions.isAdmin, (req, res, next) => {
Event.findByIdAndUpdate(req.params._id, {
$set: {
'isFinished': true
}
}).then(() => {
GeneralInfo.findOne().then(r => {
GeneralInfo.findByIdAndUpdate(r._id, {
'activeEventId': undefined,
})
}).then(() => {
res.redirect(`/admin`);
}).catch(e => console.log(e));
}).catch(e => console.log(e));
});
// register users to a Event
router.get('/registerEvent', functions.isLoggedIn, functions.isBlocked, async (req, res, next) => {
var eventId = await GeneralInfo.findOne().then(eventId => eventId.activeEventId).catch(e => console.log(e));
var currEvent = await Event.findById(eventId).catch(e => console.log(e));
const user = req.user;
if (user.accType === 'employer') {
let shouldAdd = true;
try {
const tmp = currEvent.attendants.employers.filter(e => e.id !== user._id);
tmp.length > 0 ? shouldAdd = false : shouldAdd = true;
} catch (e) {
shouldAdd = true;
}
if (shouldAdd) {
Event.findByIdAndUpdate(eventId, {
$push: {'attendants.employers': {'id': user._id, 'boothVisits': 0}, $upsert: true,}
}).then(r => {
console.log(r);
res.redirect('/event');
}).catch(e => console.log(e));
} else {
res.redirect('/event');
}
} else if (user.accType === 'seeker') {
let shouldAdd = true;
try {
const tmp = currEvent.attendants.seekers.filter(e => e.id !== user._id);
tmp.length > 0 ? shouldAdd = false : shouldAdd = true;
} catch (e) {
shouldAdd = true;
}
if (shouldAdd) {
Event.findByIdAndUpdate(eventId, {
$push: {'attendants.seekers': user._id, $upsert: true,}
}).then(r => {
console.log(r);
res.redirect('/event');
}).catch(e => console.log(e));
} else {
res.redirect('/event');
}
} else {
res.redirect('/event');
}
}
);
module.exports = router;
Upvotes: 7
Views: 13917
Reputation: 121
This happens when you pass undefined route parameters to "app.get" or "app.post" Example-
const routeName = { loginRoute: '/login',
dashboardRoute: '/member/dashboard'
};
The below is the correct usage
app.get(routeName.loginRoute, function(req, res) {
//...
});
Error occures when you use undefined routeName properties, below code throws that error, as there is no logoutRoute property in routeName object
app.get(routeName.logoutRoute, function(req, res) {
//...
});
Upvotes: 12
Reputation: 5039
Ok. This
router.use(functions.isLogisRegisteredForEventgedIn, functions.isBlocked);
was the problem. My functions names were messed up
Upvotes: 3