通过未定义的POST API的身体价值?

我正在使用mongoose在MEAN堆栈上构build一个API。 该API应该处理用户注册和身份validation。 为了testing这个,我使用扩展名为chrome的邮递员向/signup提交post请求。

 app.use("/signup", bodyParser.urlencoded({ extended: false })); app.post("/signup", Auth.userExist, function (req, res, next) { if (!req.body.email || !req.body.password) { res.json({success: false, msg: 'Please pass name and password.'}); console.log("email: " + req.body.email); console.log("password: " + req.body.password); } else { //do create new user logic... res.json({success: true, msg: 'Successful created new user.'}); } }); 

在这里,您可以看到我在向API发送请求的正文中发送的内容:

邮差

在控制台中,我得到这个:

TypeError:无法读取未定义的属性'email'

为什么我的要求没有通过?

POSTMAN中,selectx-www-form-urlencoded而不是form ,然后传递表单值。 你的代码应该工作正常。

尝试为您的端点configuration身体分析器:

 var bodyParser = require("body-parser"); app.use("/signup", bodyParser.urlencoded({ extended: false }));