Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 标识符…;已经申报了_Node.js_Typescript_Express_Postman - Fatal编程技术网

Node.js 标识符…;已经申报了

Node.js 标识符…;已经申报了,node.js,typescript,express,postman,Node.js,Typescript,Express,Postman,我正在建立一个社交网络来学习nodejs和reactjs。当前,在使用postman调试的/signin中构建后端时,我甚至无法启动节点服务器,cmd抛出以下错误: \node_react\2\nodeapi\controllers\auth.js:40 const {_id, name, email} = user; ^ SyntaxError: Identifier 'email' has already been declared 引发错误的代码段如下所示:

我正在建立一个社交网络来学习nodejs和reactjs。当前,在使用postman调试的
/signin
中构建后端时,我甚至无法启动节点服务器,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{u id,name,email}=user
称为destructuring,它将分配具有相同对象名称的新变量

因此:

const {_id, name, email} = user;
同:

const _id = user._id;
const name = user.name;
const email = user.email;
也就是说,您可能正在重新定义
email
变量,该变量由
const
关键字设置。这意味着你不能重新分配它

您可以做的是:

const {_id, name, email: _email} = user;
return res.json({token, user:{_id, name, email: _email}});

这将使
email
字段分配给
\u email
变量,避免冲突。

在代码中,您覆盖了
email
变量,该变量是使用
const
关键字创建的(aka:无法重新分配)

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

cookies
response
必须在MongoDB查询中,因此您确实有用户。

在本次分配之前,您似乎已经定义了
电子邮件
。您能提供所有
验证码吗?不相关但
新日期()+9999
将是字符串连接而不是算术。添加auth.js code@GustavoLopesThank you,更新@AlexK.Im我的案例我正在导出我导入的东西…同名…只需执行
导出{curry as curry}
为此。逻辑是外部依赖项可以全局扩展、交换和集中替换,而无需重构。具体错误:
SyntaxError:Identifier'curry'已经声明
Thank you@Jeena,这解决了这个问题,现在节点服务器已经启动,但正在使用po进行signin debugstman cmd给了我以下信息:ReferenceError:user未在exports.sign(C:\Users\luisc\node\u react\2\nodeapi\controllers\auth.js:35:38)的Layer.handle[作为handle\u请求](C:\Users\luisc\node\u react\2\nodeapi\node\u modules\express\lib\router\Layer.js:95:5)请看我的答案。非常感谢/Muito Obrigado@GustavoLopes,这完全解决了问题。不客气。作为最佳做法,在执行
/
语句时,尽量在
if
/
周围加上括号。这有助于避免混淆识别。