Я пытаюсь добавить поддокумент к родительской схеме с Мангустом и MongoDB однако я быть брошенным следующим сообщением об ошибке:Mongoose TypeError: Пользователь не конструктор
TypeError: User is not a constructor
Это базируется на документации Mongoose в на subdocuments, и я думаю, что все то же самое. Как я могу отладить это дальше?
маршрутизатор
// 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)
У меня была та же проблема. Ваше решение решило мою проблему. Спасибо за обмен – AllJs