In my models/user.js
file:
var mongoose = require(\'mongoose\');
var Schema = mongoose.Schema;
var userSchema = new Schema({
(define schema
In my case, it happened because of capital/small letter confusion. User model had this:
const userSchema = new Schema({
// ...
});
module.exports = mongoose.model('User', userSchema);
And Product model had a reference to User model but small case:
const productSchema = new Schema({
// ...
userId: {
type: Schema.Types.ObjectId,
ref: 'user', // This should exactly match the name of User model above!!!
required: true
}
});