【问题标题】:Node.js - TypeError: Cannot read property 'exec' of undefinedNode.js - TypeError:无法读取未定义的属性'exec'
【发布时间】:2021-06-14 16:24:14
【问题描述】:

我按照 youtube 上的教程编写了这段代码,但它在与他一起工作时对我不起作用!

router.post('/signup', (req,res) => {
User.findOne({ email: req.body.email })
console.log('===> 1')
.exec((error, user) => {
    console.log('===> 2')
    if(user) return res.status(400).json({
        message: 'User already registered!'
    });
    const { firstName, lastName, email, password } = req.body;
    const _user = new User({ firstName, lastName, email, password, username: Math.random().toString() });
    _user.save((error, data) => {
        if(error){
            return res.status(400).json({
                message: 'Something went wrong!'
            });
        }
        if(data){
            return res.status(201).json({
                user: data
            })
        }
    });
});});

它显示这条消息(Cannot read property 'exec' of undefined)?

【问题讨论】:

  • 将你的第一行 console.log 移到User.findOne之前

标签: javascript node.js express


【解决方案1】:

您在console.log() 函数之上调用exec() 函数,例如:console.log().exec(),这当然不存在。请再次检查您的教程,并确保在调用 exec() 之前没有遗漏需要运行的代码。

【讨论】:

  • 是的,它奏效了。谢谢你。其实是我写console.log()来检查错误,可惜console.log()本身就成了问题:)
猜你喜欢
  • 2020-05-15
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 2020-11-03
  • 2020-05-13
  • 2018-09-29
  • 2020-06-17
相关资源
最近更新 更多