TypeError: cannot read property of undefined (ExpressJS/POST) TypeError: cannot read property of undefined (ExpressJS/POST) express express

TypeError: cannot read property of undefined (ExpressJS/POST)


body-parser is not the part of express. Install it separately using npm install body-parser --save and then use it as middleware. check the code after line where you commented express.bodyParser() middleware

var cool = require('cool-ascii-faces');var express = require('express');var app = express();var pg = require('pg');var bodyParser = require('body-parser');var env = require('node-env-file');app.set('port', (process.env.PORT || 5000));app.use(express.static(__dirname + '/views/'));// views is directory for all template filesapp.set('views', __dirname + '/views');app.set('view engine', 'ejs');//app.use(express.bodyParser());app.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: false }));//DIFFERENT APPS - tells them what to doapp.post('/search', function(request, response) {    //var username = req.body;    console.log("posted something"+ request.body.searcher);    response.end("something was posted: "+ request.body.searcher);});app.get('/search', function(request, response) {     response.send("skylarr");});


There is no body property on a standard Node.JS HTTP request. That key is patched on by the bodyParser middleware.

You can either add the bodyParser middleware, or (if you don't want to parse the body for some reason) use query or URL parameters to pass searcher.


You are missing the body parser. You need to tell express to use the body parser as a middleware.

app.use(bodyParser.urlencoded({ extended: false }));app.use(bodyParser.json());

Add these two lines above the app.post().