簡體   English   中英

路徑到正則表達式拋出類型錯誤:無法讀取未定義的屬性“長度”

[英]Path-to-regexp throws TypeError: Cannot read property 'length' of undefined

所以這是錯誤消息:

/home/alex/Documents/Projects/ontario-job-portal/node_modules/path-to-regexp/index.js:63 path = ('^' + path + (strict ? '' : path[path.length - 1 ] === '/' ? '?' : '/?')) ^

類型錯誤:無法在新層 (/home/alex/Documents/Projects/ontario-job-portal/node_modules/path-to-regexp/index.js:63:49) 處讀取未定義的屬性“長度” /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) 在 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) 在 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;

當您將未定義的路由參數傳遞給“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);

是問題所在。 我的函數名稱搞砸了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM