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

4

7

I have looked at all similar questions and none are working for me. I have a node js app in which I cannot print the input text from a form, using body-parser.

My index.ejs:

         <form id="demo-2" method = "POST" action="/search">
<input type="search" name = "searcher" placeholder="Search">
                    </form>

Index.js:

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 files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

//app.use(express.bodyParser());


//DIFFERENT APPS - tells them what to do

app.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");

});

And despite using the input's name searcher I get error: TypeError: Cannot read property 'searcher' of undefined

What is wrong here?

Funky answered 14/6, 2017 at 20:19 Comment(2)
you comment out body-parser middle ware. why? un comment that and then try.Versicolor
That created an error regarding middleware and wouldn't let the app run.Funky
V
10

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 files
app.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 do

app.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");

});
Versicolor answered 15/6, 2017 at 8:8 Comment(2)
@skyguy you told in above comment if you un comment bodyParser that create an error regarding middle ware. I want to tell that bodyParser is not the part of express. install and use it separately.Versicolor
Doesn't work for me, still displaying the exact same errorSensory
N
4

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().

Neat answered 15/6, 2017 at 2:8 Comment(0)
N
1

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.

Nutation answered 14/6, 2017 at 20:35 Comment(1)
Sorry I'm new to this. Can you give an example of those 2 options?Funky
H
0

Instead of bodyparser you could use app.use(express.json()).

Hinkley answered 28/4, 2023 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.