Axios请求待处理,但在邮递员上工作

时间:2020-02-23 15:07:54

标签: javascript node.js express axios nuxt.js

我的问题是:

我正在创建Nuxt应用程序,并且正在测试API端点。

例如,当我使用Postman并通过POST发送此请求时:

localhost:3000/api/login

一切都很好,我得到了预期的结果,但是当我在客户端尝试使用此方法时:

 axios
      .post("/api/login", {
        email: email,
        password: passwort
      })
      .then(res => {
        console.log(res);
      });

在Google Chrome浏览器的“网络”标签上,我看到请求正在等待处理,我不知道为什么。

这是我的index.js文件:

  const express = require("express");
  const app = express();
  const mongoose = require("mongoose");
  const bodyParser = require("body-parser");

  mongoose.connect(
    "mongodb+srv://mydata/realtime?retryWrites=true&w=majority",
    {
      useNewUrlParser: true,
      useUnifiedTopology: true
    }
  );


  app.use(bodyParser.json());
  const routes = require("./routes/routes");
  app.use(routes);
  module.exports = {
    path: "/api",
    handler: app
  };

这是我的路线文件夹:

const { Router } = require("express");
const router = Router();

import createUser from "../controllers/user";
import checkLogin from "../controllers/login";
/* GET users listing. */

router.post("/login", checkLogin);
router.post("/register", createUser);

module.exports = router;

和我的login.js文件:

const bcrypt = require("bcrypt");
import User from "../models/user";
export default async function checkLogin(req, res) {
  const user = await User.findOne({ email: req.body.email });
  if (!user) {
    return res.status(404).json({
      success: false,
      message: "Diese E-Mail konnte nicht verifiziert werden."
    });
  }
  const checkPw = bcrypt.compareSync(req.body.password, user.password);
  if (checkPw) {
    res.status(200).json({
      success: true,
      message: "Erfolgreich eingeloggt"
    });
  } else {
    return res.status(403).json({
      success: false,
      message: "Falsche Passwort"
    });
  }
}

编辑:

我已经找到了问题,我在传递的数据中忘记了this关键字:

 axios
  .post("/api/login", {
    email: this.email,
    password: this.passwort
  })
  .then(res => {
    console.log(res);
  });

0 个答案:

没有答案