0
votes

while running my first MEAN stack application i am getting this error:

\contactlist\node_modules\express\lib\router\index.js:502 this.stack.push(layer); ^

TypeError: Cannot read property 'push' of undefined at Function.route (C:\Users\Mahima Kaushik\Desktop\contactlist\node_modules\express\lib\router\index.js:502:14) at Function.proto. [as post] (C:\Users\Mahima Kaushik\Desktop\contactlist\node_modules\express\lib\router\index.js:509:22) at Object. (C:\Users\Mahima Kaushik\Desktop\contactlist\routes\route.js:11:8) at Module._compile (internal/modules/cjs/loader.js:1251:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10) at Module.load (internal/modules/cjs/loader.js:1100:32) at Function.Module._load (internal/modules/cjs/loader.js:962:14) at Module.require (internal/modules/cjs/loader.js:1140:19) at require (internal/modules/cjs/helpers.js:75:18) at Object. (C:\Users\Mahima Kaushik\Desktop\contactlist\app.js:9:15) at Module._compile (internal/modules/cjs/loader.js:1251:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10) at Module.load (internal/modules/cjs/loader.js:1100:32) at Function.Module._load (internal/modules/cjs/loader.js:962:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47

contactlist/app.js

var express = require('express');
var mongoos = require('mongoose');
var bodyparser  = require('body-parser');
var cors = require('cors');
var path = require('path');

var app = express();
const route = require('./routes/route');

//connect to mongodb
mongoos.Mongoose.connect('mongodb://localhost:27017/contactlist');

//on connection
Mongoose.connection.on('connected',()=>{
    console.log('connected to database mongodb @2717');
});
Mongoose.connection.on('error',(err)=>{
    if(err)
    {
        console.log('error in database connection:'+err);
    }
});


//port number
const port = 3000
//adding middleware -cors
app.use(cors());

//boday-parser
app.use(bodyparser.json());

//static files
app.use(express.static(path.join(__dirname,'public')));

//add routes
app.use('/api',route);
//testing
app.get('/',(req,res)=>{
    res.send('contactlist');
});

app.listen(port,()=>{
    console.log('server started at port:'+port);

});

contactlist/routes/route.js

const { Router } = require('express');
const express = require("express");
const router = express.Router();
//retrieving contacts
router.get('/contacts',(req,res,next) =>{
    res.send('retrieving the contact list');

});

//add contacts
Router.post('/contact',(req , res, next)=>{
//logic
});

//delete contacts
Router.delete('/contact/:id',(req , res, next)=>{
//logic
});

module.exports = router;
1
You missed mongoos before Mongoose.connection? try like this mongoos.Mongoose.connectionNaren

1 Answers

1
votes

You didn't create route correctly. The .get, .post and .delete are instance methods of Router instance, NOT static method. For more info, see Routing

routes/route.js should be:

const { Router } = require('express');
const router = Router();
//retrieving contacts
router.get('/contacts', (req, res, next) => {
  res.send('retrieving the contact list');
});

//add contacts
router.post('/contact', (req, res, next) => {
  //logic
});

//delete contacts
router.delete('/contact/:id', (req, res, next) => {
  //logic
});

module.exports = router;