亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

API 呼叫後 Axios POST 傳回:ERR_EMPTY_RESPONSE
P粉014218124
P粉014218124 2024-04-03 14:16:50
0
1
710

我正在嘗試在 Node.JS 應(yīng)用程式中建立註冊(cè)表單。

當(dāng)我發(fā)出 AXIOS post 請(qǐng)求時(shí),資料會(huì)儲(chǔ)存到我的資料庫中,但實(shí)際的承諾仍然傳回未定義。有效負(fù)載已正確填滿。更令人困惑的是,我基本上已經(jīng)在功能中鏡像了我的登入程式碼,並且運(yùn)行完美。

下面的程式碼用於註冊(cè)。

import axios from 'axios';
import { showAlert } from './alerts';

export const signup = async (name, email, password, passwordConfirm) => {
  try {
    const res = await axios({
      method: 'POST',
      url: 'http://127.0.0.1:3000/api/v1/users/signup',
      data: {
        name,
        email,
        password,
        passwordConfirm,
      },
    }) // res returns undefined.

    if (res.data.status === 'success') {
      showAlert('success', 'Signed up successfully!');
      window.setTimeout(() => {
        location.assign('/login');
      }, 1500);
    }
  } catch (err) {
    showAlert('error', err.response.data.message);
  }
};

這是我的使用者路由器,位於 http://127.0.0.1:3000/api/v1/users

const authController = require('./../controllers/authController');
// const reviewController = require('./../controllers/reviewController');

const router = express.Router();

router.post('/signup', authController.signup);

這是我的帶有註冊(cè)功能的 authController。

const signToken = (id) => {
  return jwt.sign({ id: id }, process.env.JWT_SECRET, {
    expiresIn: process.env.JWT_EXPIRES_IN,
  });
};

const createSendToken = (user, statusCode, res) => {
  const token = signToken(user._id);
  const cookieOptions = {
    expires: new Date(
      Date.now() + process.env.JWT_COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000
    ),
    httpOnly: true,
  };

  if (process.env.NODE_ENV === 'production') cookieOptions.secure = true;

  res.cookie('jwt', token, cookieOptions);

  // Remove password from output
  user.password = undefined;

  res.status(statusCode).json({
    status: 'success',
    token,
    data: {
      user,
    },
  });
};

exports.signup = catchAsync(async (req, res, next) => {
  const newUser = await User.create({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password,
    passwordConfirm: req.body.passwordConfirm,
    passwordChangedAt: req.body.passwordChangedAt,
    role: req.body.role,
  });
  const url = `${req.protocol}://${req.get('host')}/me`;
  await new Email(newUser, url).sendWelcome();

  createSendToken(newUser, 201, res);
});

非常感謝。

在我看來,由於我的身份驗(yàn)證控制器正在發(fā)送 201 回應(yīng),因此這種情況不應(yīng)該發(fā)生。

P粉014218124
P粉014218124

全部回覆(1)
P粉268654873

then() 方法不會(huì)傳回可等待的 Promise,而是採用一個(gè)回呼函數(shù),該回呼函數(shù)將在請(qǐng)求完成時(shí)執(zhí)行。 你需要改變這個(gè):

 const res = await axios({
      method: 'POST',
      url: 'http://127.0.0.1:3000/api/v1/users/signup',
      data: {
        name,
        email,
        password,
        passwordConfirm,
      },
    }).then(console.log('hello world', res, 'hello')); // res returns undefined.

進(jìn)入此:

const res = await axios({
  method: 'POST',
  url: 'http://127.0.0.1:3000/api/v1/users/signup',
  data: {
    name,
    email,
    password,
    passwordConfirm,
  },
});

console.log('hello world', res, 'hello'); 

if (res.data.status === 'success') {
  showAlert('success', 'Signed up successfully!');
  window.setTimeout(() => {
    location.assign('/login');
  }, 1500);
}
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板