邮递员发布请求无法读取未定义的属性

Posted

技术标签:

【中文标题】邮递员发布请求无法读取未定义的属性【英文标题】:Postman post request cannot read property of undefined 【发布时间】:2019-05-03 20:15:03 【问题描述】:

我在我的 express 服务器中有一个路由设置,它发送一个 post 请求并在我的 mongoDB 数据库中添加一条新消息。

当我将名称静态添加到正文字段时;即new userMessage = new Message( name: 'Joseph', email: 'joseph@gmail.com', message: 'Hello Joseph'),发帖成功。

但是当它是动态的;即new userMessage = new Message( name: req.body.name, email: req.body.email, message: req.body.message) 它会引发错误。

我已尝试多次调试该问题,但均未成功。

错误

TypeError: Cannot read property 'name' of undefined
    at router.post (C:\Users\Joseph\Documents\Hackathons\hack.api\app\routes\message.js:13:24)
    at Layer.handle [as handle_request] (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:275:10)
    at cors (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\cors\lib\index.js:188:7)

Message.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const MessageSchema = new Schema(
    name:  type: String, required: true ,
    email:  type: String, required: true ,
    message:  type: String, required: true 
);

module.exports = Message = mongoose.model('message', MessageSchema);

Server.js

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cors = require('cors');

const message =  require('../routes/message');

const app = express();

//Enable cors
app.use(cors( origin: '*' ));

//Connect to mongoose
mongoose.connect('mongodb://localhost/messages',  useNewUrlParser: true )
    .then(() => console.log('MongoDB connected'))
    .catch(err => console.log(err));

//Use routes
app.use('/message', message);

//Body Parser middleware
app.use(bodyParser.urlencoded(extended: false));
app.use(bodyParser.json());

//Test route
app.get('/', (req, res) => 
    res.send('Hello world');
)

//Set server port
const port = process.env.PORT || 5000;

app.listen(port, () => 
    console.log(`Server started on port $port`);
);  

message.js

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

//Load message model
const Message = require('../models/Message');

router.get('/test', (req, res) =>
    res.json( msg: 'Message post route works' )
);

router.post('/post', (req, res) => 
    const userMessage = new Message(
        name: req.body.name,
        email: req.body.email,
        message: req.body.message
    );

    userMessage.save()
        .then(message => res.json(message))
        .catch(err => console.log(err))
);

module.exports = router;

【问题讨论】:

【参考方案1】:

我通过改变解决了它

//Use routes
app.use('/message', message);

//Body Parser middleware
app.use(bodyParser.urlencoded(extended: false));
app.use(bodyParser.json());

//Body Parser middleware
app.use(bodyParser.urlencoded(extended: false));
app.use(bodyParser.json());

//Use routes
app.use('/message', message);

我猜顺序很重要。在服务器配置顺序中确实很重要。

【讨论】:

【参考方案2】:

最好在创建新的 Mongo 对象之前获取数据并对其进行验证:

const  body: name, email, message   = req;
//validate email for example or you just logs it to know data reviced or not? 
const userMessage = new Message(
        name: name,
        email: email,
        message: message
    );

【讨论】:

【参考方案3】:

如果您从客户端发送嵌套对象,例如 person[name] = 'cw',那么您必须编写 app.use(bodyParser.urlencoded(extended: true));,或者如果不是这种情况,请确保您在 html 表单中的输入具有 name="file" 属性。

【讨论】:

以上是关于邮递员发布请求无法读取未定义的属性的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:无法读取未定义的nodejs请求http的属性'0'

jsonp 请求中的“未捕获的类型错误:无法读取未定义的属性‘toLowerCase’”

未捕获的类型错误:无法读取未定义的属性 toLowerCase

MERN - TypeError:无法读取未定义的属性“id”

TypeError:无法读取未定义的属性(读取“地图”),以及 React.js 中的多个 API 请求

引导自动完成错误:无法读取未定义的属性“”