我看過所有類似的問題,沒有人爲我工作。我有一個節點js應用程序,我無法使用body-parser從表單輸入文本。Node js post - 無法讀取undefined的屬性?
我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");
});
而且,儘管使用輸入的名字搜索,我得到錯誤:TypeError: Cannot read property 'searcher' of undefined
這裏有什麼問題?
你註釋掉了body-parser中間件。爲什麼?取消評論,然後嘗試。 – Khurram
這造成了有關中間件的錯誤,不會讓應用程序運行。 – skyguy