最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 3浏览 0评论

我的请求正文在节点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());

我的请求正文在节点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. 暂无评论