how to fix “can't read property push of undefined” error in Nodejs?

断了今生、忘了曾经 提交于 2020-05-15 03:14:52

问题


I have coded a simple app to learn Nodejs but when i run "nodemon index.js" in cmd i have this error TypeError: Cannot read property 'push' of undefined app crashed - waiting for file changes before starting...

i have follow all instruction in the udemy course for learn nodejs and i faced this problem when i separated the file into two files index.js and genres.js

genres.js

const express = require('express');
const router = express.Router;

//simple data 
const genres = [{
        id: 1,
        name: 'course1'
    },
    {
        id: 2,
        name: 'course2'
    },
    {
        id: 3,
        name: 'course3'
    }
];

//////////////////////////////////////////////////////////////////////
/////////////////////////////////// Get //////////////////////////////
//////////////////////////////////////////////////////////////////////


router.get('/', (req, res) => {
    res.send(genres);
});

router.get('/:id', (req, res) => {

    const genre = genres.find(c => c.id ===
        parseInt(req.params.id)); //req.params.id return string 
    if (!genre)
        return res.status(404).send('The course is not found...');

    res.send(genre);

    res.send(req.params.id);
});

router.get('/:year/:month', (req, res) => {
    res.send(req.params);
});

router.post('/', (req, res) => {
    const {
        error
    } = validategenre(req.body);
    if (error)
        return res.status(400).send(error.details[0].message);

    const genre = {
        id: genres.length + 1,
        name: req.body.name
    }
    genres.push(genre);
    res.send(genre);
});

router.put('/:id', (req, res) => {

    const genre = genres.find(c => c.id === parseInt(req.params.id));
    if (!genre)
        return res.status(404).send('The course does not exist !!! ');

    const result = validategenre(req.body);
    if (result.error)
        return res.status(400).send(result.error.details[0].message);


    genre.name = req.body.name;
    res.send(genre);

});

function validategenre(genre) {

    const schema = {
        name: Joi.string().min(3).required()
    };
    return Joi.validate(genre, schema);

}

module.exports = router;

index.js

const Joi = require('joi');
const genres = require('./routes/genres');
const express = require('express');
const app = express();


app.use(express.json());
app.use('/api/genres', genres);
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listining on port ${port}...`));

回答1:


In genres.js, you should import

const router = express.Router();

instead of

const router = express.Router;

Also, the error you mention could be from any push in your code (without any more info), please specify the stacktrace next time :)



来源:https://stackoverflow.com/questions/53902424/how-to-fix-cant-read-property-push-of-undefined-error-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!