Javascript regexp的路径抛出TypeError:无法读取属性';长度';未定义的

Javascript regexp的路径抛出TypeError:无法读取属性';长度';未定义的,javascript,node.js,npm,Javascript,Node.js,Npm,这就是错误信息: /home/alex/Documents/Projects/ontario作业门户/node_模块/regexp/index.js路径:63 path=('^'+path+(严格?'':path[path.length-1]=='/'?':'/'?')) ^ TypeError:无法读取未定义的属性“length” 在pathtoRegexp(/home/alex/Documents/Projects/antario job portal/node_modules/path t

这就是错误信息:

/home/alex/Documents/Projects/ontario作业门户/node_模块/regexp/index.js路径:63 path=('^'+path+(严格?'':path[path.length-1]=='/'?':'/'?')) ^

TypeError:无法读取未定义的属性“length” 在pathtoRegexp(/home/alex/Documents/Projects/antario job portal/node_modules/path to regexp/index.js:63:49) 在新层(/home/alex/Documents/Projects/anthontario job portal/node_modules/express/lib/router/Layer.js:45:17) 在Function.use(/home/alex/Documents/Projects/androtario job portal/node_modules/express/lib/router/index.js:464:17) 反对。(/home/alex/Documents/Projects/ontario作业门户/routes/event.js:11:8) 编译(Module.js:652:30) 在Object.Module.\u extensions..js(Module.js:663:10) 在Module.load(Module.js:565:32) 在tryModuleLoad时(module.js:505:12) 在Function.Module.\u加载(Module.js:497:3) at Module.require(Module.js:596:17) 根据需要(内部/module.js:11:18) 反对。(/home/alex/Documents/Projects/ontario job portal/app.js:15:13) 编译(Module.js:652:30) 在Object.Module.\u 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;
嗯。这个

router.use(functions.isLogisRegisteredForEventgedIn, functions.isBlocked);

这就是问题所在。我的函数名被弄乱了

当您将未定义的路由参数传递给“app.get”或“app.post”时,会发生这种情况 范例-

下面是正确的用法

app.get(routeName.loginRoute, function(req, res) {
//...
});
使用未定义的routeName属性时出错,下面的代码抛出该错误,因为routeName对象中没有logoutRoute属性

app.get(routeName.logoutRoute, function(req, res) {
//...
});
app.get(routeName.logoutRoute, function(req, res) {
//...
});