这是错误消息:
/home/alex/Documents/Projects/ontario-job-portal/node_modules/path-to-regexp/index.js:63 path =('^'+ path +(strict?'':path [path.length-1] ==='/'?'?':'/?')) ^
TypeError:无法读取未定义的属性“ length” 在pathtoRegexp(/home/alex/Documents/Projects/ontario-job-portal/node_modules/path-to-regexp/index.js:63:49) 在新层(/home/alex/Documents/Projects/ontario-job-portal/node_modules/express/lib/router/layer.js:45:17) 在Function.use(/home/alex/Documents/Projects/ontario-job-portal/node_modules/express/lib/router/index.js:464:17) 在对象。 (/home/alex/Documents/Projects/ontario-job-portal/routes/event.js:11:8) 在Module._compile(module.js:652:30) 在Object.Module._extensions..js(module.js:663:10) 在Module.load(module.js:565:32) 在tryModuleLoad(module.js:505:12) 在Function.Module._load(module.js:497:3) 在Module.require(module.js:596:17) 在要求时(internal / module.js:11:18) 在对象。 (/home/alex/Documents/Projects/ontario-job-portal/app.js:15:13) 在Module._compile(module.js:652:30) 在Object.Module._extensions..js(module.js:663:10) 在Module.load(module.js:565:32) 在tryModuleLoad(module.js:505:12)
如果我去应该抛出错误的app.js中的位置,我会看到以下内容:
var event = require('./routes/event');
这是正确的路径。如果我输入的路径不正确,则会显示以下信息:
Error: Cannot find module './routes/events'
因此路径正确。
如果我注释掉这段代码,则代码运行。但不幸的是,我需要这种导入才能工作。注释掉整个文件无济于事(抛出相同长度的未定义错误)。
重新安装所有节点模块也无济于事。
这是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;
答案 0 :(得分:1)
好的。这个
router.use(functions.isLogisRegisteredForEventgedIn, functions.isBlocked);
是问题所在。我的函数名称搞砸了
答案 1 :(得分:0)
当您将未定义的路由参数传递给“ app.get”或“ app.post”时,会发生这种情况 例子-
const routeName = { loginRoute: '/login',
dashboardRoute: '/member/dashboard'
};
以下是正确的用法
app.get(routeName.loginRoute, function(req, res) {
//...
});
当您使用未定义的routeName属性时发生错误,下面的代码将引发该错误,因为routeName对象中没有logoutRoute属性
app.get(routeName.logoutRoute, function(req, res) {
//...
});