5

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
  • You might consider using consistent indentation when writing code - it'll make read and debugging it much easier, not only for potential answerers, but for you as well. Dec 23, 2018 at 9:11

3 Answers 3

13

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 :)

1
  • 3
    Thank you very much it finally worked i can't imagine that was the problem it's a first time to code NodeJS , and i will specify the stack trace next time :)) Dec 23, 2018 at 11:17
0

Use const router = express.Router() instead of const router = express.Router.

0
0

It can be wrong variable name :)

I made a mistake due to intelligence in my quick sample -

Typically signature of a ReST call is router.get("/something", (req: any, res: any, next: any) => { response.write("test"); response.end(); });

Here if you will notice I was using response where as i was suppose to use res

Moreover ensure you have registered your routes using app.use(router);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.