Path-to-regexp 抛出 TypeError:无法读取未定义的属性“length”
因此,这是错误消息:
/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)
如果我转到 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;
当您将未定义的路由参数传递给“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) {
//...
});
好的。这就是
router.use(functions.isLogisRegisteredForEventgedIn, functions.isBlocked);
问题所在。我的函数名称搞乱了