简体   繁体   English

ReferenceError:未定义err

[英]ReferenceError: err is not defined

I'm started working on my very first API using Mongo, Express and Node. 我开始使用Mongo,Express和Node开发第一个API。 When i tried to make API endponit for one specific user, console throw error ReferenceError: err is not defined . 当我尝试为一个特定用户设置API endponit时,控制台抛出错误ReferenceError: err is not defined An error appears in the method I already used for auth part, and there it worked fine. 我已经用于身份验证部分的方法中出现错误,并且可以正常工作。 The part of code where is the error, on line 5: 错误的代码部分在第5行:

exports.userById = (req, res, next, id) => {
   User.findById(id).exec(() => {
       if(err || !user) {
           return res.status(400).json({
               err: "User not found"
           });
       }
       req.profile = user //adds profile object in req with user info
       next();
   });
}

Also, the part of code where I tried to get a single user: 另外,我尝试获得单个用户的代码部分:

exports.getUser = (req, res) => {
    req.profile.hashed_password = undefined;
    req.profile.salt = undefined;
    return res.json(req.profile); 
}

I don't think the problem could be here, but there is also route line from routes file 我认为问题可能不在这里,但是路由文件中也有路线

router.get("/users/:userId", authController.requireSignin, userController.getUser);

Thanks everyone for the help! 谢谢大家的帮助!

我很确定err来自exec

User.findById(id).exec(err => {...});

Edit I guess you want to search by id and return something. 编辑我想您想按ID搜索并返回一些内容。 Try this. 尝试这个。

 User.findById(id, (err, user) => {

    // if error display errort
    if(err) console.error(err);

    // if user do not exists
    if(!user) {// what is user ? the doc result ?
        return res.status(400).json({
            "err": "User not found"   // i think use ""
        });
    }

    req.profile = user //adds profile object in req with user info
    next();

 });

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

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