繁体   English   中英

nodejs mongoDB连接登录页面

[英]nodejs mongoDB connection login page

当我通过 postman 发送用户名、密码、email 时,它可以正常工作并将信息添加到 mongodb 模式,但在 React 的登录页面中它不起作用。 如果有人能提供帮助,我将不胜感激。 太感谢了。

安慰

反应逻辑:

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");
})

确保您将请求发送到正确的路由/服务器,也许您输入错误的端口或路由本身。

我相信这一行const res = await axios.post("/auth/register", {不匹配你的后端app.use("/api/auth", authRoute);

看起来您在/register端点上定义了注册逻辑,但您正在向/auth/register端点发送请求。

尝试更改您的 React 逻辑以将请求发送到/register端点:

const res =  await axios.post("/register", {
  username,
  email,
  password,
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM