gpt4 book ai didi

node.js - 标识符...已经声明

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:15 25 4
gpt4 key购买 nike

我正在建立一个社交网络来学习nodejs和reactjs。目前,在使用 postman 在 /signin 调试中构建后端时,我什至无法启动 Node 服务器,cmd 会抛出以下错误:

\node_react\2\nodeapi\controllers\auth.js:40
const {_id, name, email} = user;
^
SyntaxError: Identifier 'email' has already been declared

引发错误的代码片段如下:

//generate a token with user id and secret
const token = jwt.sign({_id: user._id}, process.env.JWT_SECRET);

//persist the token as 't' in cookie with expiry date
res.cookie("t", token, {expire: new Date() + 9999});

//return response with user and token to frontend client
const {_id, name, email} = user;
return res.json({token, user:{_id, email, name}});

完整的 auth.js 代码如下:

const jwt = require("jsonwebtoken");
require ('dotenv').config();
const User = require("../models/user");

exports.signup = async (req, res) => {
const userExists = await User.findOne({email: req.body.email});
if(userExists)
return res.status(403).json({
error: "Email is taken!"
});
const user = await new User(req.body);
await user.save();
res.status(200).json({ message: "Signup success! Please login:)" });
};

exports.signin = (req,res) => {
//find the user based on email
const { email, password } = req.body
User.findOne({email}, (err, user) => {
//if error or no user
if (err || !user) {
return res.status(401).json({
error: "User with that email does not exists. Please signin."
});
}
//if user is found make sure the email and password match
// create authenticate method in model and use here
if (!user.authenticate(password))
return res.status(401).json({
error: "Email and password do not match."
});
})

//generate a token with user id and secret
const token = jwt.sign({_id: user._id}, process.env.JWT_SECRET);

//persist the token as 't' in cookie with expiry date
res.cookie("t", token, {expire: new Date() + "9999"});
//return response with user and token to frontend client
const {_id, name, email} = user;
return res.json({token, user:{_id, email, name}});
}

最佳答案

在您的代码中,您将覆盖使用 const 关键字创建的 email 变量(又名:无法重新分配)。

Jeena的回答可能解决了问题,但看来你有更深层次的问题。

User.findOne({email}, (err, user){} 是异步的。因此此代码下面的代码可能会失败。

您可能必须将代码重写为:

exports.signin = (req,res) => {
//find the user based on email
const { email, password } = req.body
User.findOne({email}, (err, user) => {
//if error or no user
if (err || !user) {
return res.status(401).json({
error: "User with that email does not exists. Please signin."
});
}
//if user is found make sure the email and password match
// create authenticate method in model and use here
if (!user.authenticate(password))
return res.status(401).json({
error: "Email and password do not match."
});

//generate a token with user id and secret
const token = jwt.sign({_id: user._id}, process.env.JWT_SECRET);

//persist the token as 't' in cookie with expiry date
res.cookie("t", token, {expire: new Date() + "9999"});
//return response with user and token to frontend client
const {_id, name, email} = user;
return res.json({token, user:{_id, email, name}});
})
}

cookieresponse 必须位于 MongoDB 查询内,因此您确实拥有用户。

关于node.js - 标识符...已经声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com