Giter Club home page Giter Club logo

Comments (3)

Amal-Gendia avatar Amal-Gendia commented on July 23, 2024 4

Thank you @lange I already asked the question on stackoverflow and get the answer on it the error was
in this line const router = express.Router ;
it must be const router = express.Router();

from nodecg-obs.

anarcho-loneliness avatar anarcho-loneliness commented on July 23, 2024

Hi @AmolaMansour,

I'm a little confused by this issue, it doesn't seem that your code is using nodecg-obs. Did you mean to open this issue on the https://github.com/nodejs/node repo?

You may also have luck asking this question on https://stackoverflow.com/

Best,
Alex

from nodecg-obs.

Amal-Gendia avatar Amal-Gendia commented on July 23, 2024

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}...`));`

from nodecg-obs.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.