最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我的请求正文在节点API中显示为空或未定义

运维笔记admin14浏览0评论

我的请求正文在节点API中显示为空或未定义

我的请求正文在节点API中显示为空或未定义

这是我用来验证我的登录名的API,但未收到正确的请求正文。

const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

     router.post("/login", (req, res) => {
          const email = req.body.email;
          const password = req.body.password;
          //Find user by email
          if (email == "" || email === undefined) {
            console.info("Email is empty");
          }

          if (password == "" || password === undefined) {
            console.info("Password is empty");
          }

          User.findOne({ email }).then(user => {
            if (!user) {
              return res.status(404).json({ email: "user not found" });
            } else {
              //Check Password
              bcryptpare(password, user.password).then(isMatch => {
                if (isMatch) {
                  res.json({ msg: "Success" });
                } else {
                  return res.status(400).json({ password: "Incorrect Password" });
                }
              });
            }
          });
        });

我正在编写一种类似的API来注册我的用户,这非常正常。

回答如下:

使用[email protected]版本,body-parser中间件与express捆绑在一起。

因此您必须使用以下行:

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

而不是

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
发布评论

评论列表(0)

  1. 暂无评论