Mongoose TypeError:用户不是一个构造函数
我试图添加一个子文档与Mongoose和MongoDB的父架构,但是我被引发以下错误:
TypeError: User is not a constructor
这是基于Mongoose关于子文档的文档 ,我认为一切都是一样的。 我怎样才能进一步debugging呢?
路由器
// Add a destination to the DB router.post('/add', function(req, res, next) { let airport = req.body.destination let month = req.body.month let id = (req.user.id) User.findById(id , function (err, User) { if (err) return handleError(err) function addToCart (airport, month, id) { var user = new User ({ destinations: [( airport = '', month = '' )] }) dog.destinations[0].airport = airport dog.destinations[0].month = month dog.save(callback) res.status(200).send('added') } addToCart() }) console.log(airport) })
架构
var destinationSchema = new Schema({ airport: String, month: String }) // Define the scheme var User = new Schema ({ firstName: { type: String, index: true }, lastName: { type: String, index: true }, email: { type: String, index: true }, homeAirport: { type: String, index: true }, destinations: [destinationSchema] }) User.plugin(passportLocalMongoose) module.exports = mongoose.model('User', User)
JavaScript对variables名称是区分大小写的。 User
模型和User
结果具有相同的名称。
您的代码将与以下更改一起工作:
User.findById(id , function (err, user) { /* ^ use small `u` */ if (err) return handleError(err) /* rest of your code */
另外请记住,在你的代码中,你正在声明另一个名为user
variables。 你将需要改变一些不同的东西。