我正在按照 YouTube 视频的步骤制作一个 React 博客应用程序。我已经正确构建了整个项目,当我按下提交按钮时出现问题,它显示以下错误:
错误TypeError:无法读取未定义的属性(读取“body”)
注册时 (file:///C:/Users/SAAD%20COMMUNICATION/OneDrive/Desktop/InsightInk/InsightInk/api/controllers/auth.js:9 :20)
位于 Layer.handle [作为 handle_request] (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外层\layer.js:95:5)
在下一个 (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外 oute.js:144:13)
位于 Route.dispatch (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外 oute.js:114:3)
位于 Layer.handle [作为 handle_request] (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外层\layer.js:95:5)
位于 C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外部\index.js:284:15
位于 Function.process_params (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外部\index.js:346:12)
在下一个 (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外部\index.js:280:10)
位于 Function.handle (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外部\index.js:175:3)
在路由器 (C:\Users\SAAD COMMUNICATION\OneDrive\Desktop\InsightInk\InsightInk pi ode_modules xpress\lib 外部\index.js:47:12)
//auth.js
import { db } from "../db.js";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
export const register = ({req, res}) => {
//CHECK EXISTING USER
const q = "SELECT * FROM users WHERE email = ? OR username = ?";
db.query(q, [req.body.email, req.body.username], (err, data) => {
if (err) return res.status(500).json(err);
if (data.length) return res.status(409).json("User already exists!");
//Hash the password and create a user
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(req.body.password, salt);
const q = "INSERT INTO users(`username`,`email`,`password`) VALUES (?)";
const values = [req.body.username, req.body.email, hash];
db.query(q, [values], (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json("User has been created.");
});
});
};
export const login = ({req, res}) => {
//CHECK USER
const q = "SELECT * FROM users WHERE username = ?";
console.log(req.body);
db.query(q, [req.body.username], (err, data) => {
if (err) return res.status(500).json(err);
if (data.length === 0) return res.status(404).json("User not found!");
//Check password
const isPasswordCorrect = bcrypt.compareSync(
req.body.password,
data[0].password
);
if (!isPasswordCorrect)
return res.status(400).json("Wrong username or password!");
const token = jwt.sign({ id: data[0].id }, "jwtkey");
const { password, ...other } = data[0];
res
.cookie("access_token", token, {
httpOnly: true,
})
.status(200)
.json(other);
});
};
export const logout = ({req, res}) => {npm
res.clearCookie("access_token",{
sameSite:"none",
secure:true
}).status(200).json("User has been logged out.")
};
注册和登录函数应该有两个参数,例如
export const register = (req, res) => { }