Mongoose TypeError: User is not a constructor

前端 未结 1 1440
清酒与你
清酒与你 2020-12-10 17:59

I\'m trying to add a subdocument to a parent schema with Mongoose and MongoDB however I\'m being thrown the following error:

TypeError: User is not a constru         


        
相关标签:
1条回答
  • 2020-12-10 18:41

    JavaScript is case sensitive about the variable names. You have User model and the User result with the same name.

    Your code will work with the following change :

       User.findById(id , function (err, user) {
    /*                                   ^ use small `u` */
           if (err) return handleError(err)
    
    /* rest of your code */
    

    Also keep in mind that further in your code you are declaring another variable named user. You will need to change that to something different.

    0 讨论(0)
提交回复
热议问题