Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'push' of undefined -Express

I have written two Javascript files for nodejs application which uses express to route and for the creation of server.As i am beginner i don't know much about it. When i am running the application,it is showing me the following error :-

/usr/lib/node_modules/express/lib/router/index.js:472
    this.stack.push(layer);
              ^

TypeError: Cannot read property 'push' of undefined
    at Function.use (/usr/lib/node_modules/express/lib/router/index.js:472:15)
    at Object.<anonymous> (/home/dhiresh/Coding/angularjsapp/api.js:9:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/dhiresh/Coding/angularjsapp/index.js:2:7)

index.js file is as follows :-

var express = require('express'),
api = require('./api'),
app=express(); 

app
.use(express.static('./public'))
.use('/api',api)
.get('*',function (req,res) {
    res.sendfile('public/index.html');
}).listen(3000);

api.js file is as follows :-

var express = require('express'),
 bourne = require('bourne'),
 bodyParser= require('body-parser'),

 db = new bourne('data.json'),

 router =express.Router(); 

 router
 .use(function(req,res,next){
    if(!req.user) req.user={ id:1 };
    next();
 })
 .use(bodyParser.json())
 .route('/contact')
 .get(function(req,res){
    db.find({userId : parseInt(req.user.id,10)},function(err,data){
        res.json(data);
    });
 })
 .post(function(req,res){
    var contact = req.body;
    contact.userId = req.user.id;

    db.insert(contact,function(err,data){
        res.json(data);
    });
 });

 router
 .param('id',function(req,res,next){
    req.dbQuery = { id:parseInt(req.params.id,10) }
 })
 .route('/contact/:id')
 .get(function(req,res){
    db.findOne(req.dbQuery,function(err,data){
        res.json(data);
    });
 })
 .put(function(req,res){
        var contact = req.body;
        delete contact.$promise;
        delete contact.$resolve;
        db.update(req.dbQuery,function(err,data){
        res.json(data[0]);
    })
    })
 .delete(function(req,res){
    db.delete(req.dbQuery,function(err,data){
        res.json(null);         
    })
 });

module.exports = router;

Could anyone tell me the answere for this.

like image 399
Dhiresh Budhiraja Avatar asked Aug 13 '17 13:08

Dhiresh Budhiraja


3 Answers

My problem was missing parentheses:

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

Changing this to

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

solved my problem.

like image 113
Mahdieh Shavandi Avatar answered Oct 14 '22 16:10

Mahdieh Shavandi


This error occurs whenever you import Router from the Express module and then use the variable directly instead of reassigning it.

BAD CODE

import { Router as router} from express;

router.get('/', ( req, res) => {
  return res.send(req.context.models.users[req.context.me.id]);
});

There are other ways these bad codes could have occurred but the rule is DON'T USE THE ROUTER VARIABLE DIRECTLY , REASSIGN IT

GOOD CODE

import { Router as expressRouter } from 'express';

const router = expressRouter();

router.get('/', (req, res) => {
  return res.send(req.context.models.users[req.context.me.id]);
});

OR

import { Router } from 'express';

const router = Router();

router.get('/', (req, res) => {
  return res.send(req.context.models.users[req.context.me.id]);
});
like image 23
Akintunde Avatar answered Oct 14 '22 16:10

Akintunde


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

router.get('/', (req, res) => res.send('Root route'))

module.exports = router;

the above code should work.

like image 1
n.ramesh Naveenkumar Avatar answered Oct 14 '22 15:10

n.ramesh Naveenkumar