當我通過郵遞員發送用戶名、密碼、電子郵件時,它可以作業并將資訊添加到 mongodb 模式,但在登錄頁面中反應它不起作用。如果有人可以提供幫助,我將不勝感激。太感謝了。
安慰
反應邏輯:
const [username, setUserName] = useState("");
const [password, setPassword] = useState("");
const [email, setEmail] = useState("");
const handleSubmit = async (e) =>{
console.log(".....");
e.preventDefault();
const res = await axios.post("/auth/register", {
username,
email,
password,
});
console.log(res)
};
Node.js 邏輯:
router.post("/register", async (req, res) => {
console.log("here")
try{
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: hashedPassword,
});
const user = await newUser.save();
res.status(200).json(user);
} catch(err) {
console.log(err);
}
}
);
/////////////////////
app.use("/api/auth", authRoute);
app.use("/",(req,res)=>{
console.log("main url")
})
app.listen("3000",()=>{
console.log("backend running");
})
uj5u.com熱心網友回復:
確保您將請求發送到正確的路由/服務器,也許您錯過了輸入埠或路由本身。
uj5u.com熱心網友回復:
我相信這條線const res = await axios.post("/auth/register", {與您的后端不匹配app.use("/api/auth", authRoute);
uj5u.com熱心網友回復:
看起來您在/register端點上定義了注冊邏輯,但是您正在向端點發送請求/auth/register。
嘗試更改您的 React 邏輯以將請求發送到/register端點:
const res = await axios.post("/register", {
username,
email,
password,
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/440865.html
標籤:javascript 节点.js 反应 mongodb 表示
