Hi, I wrote two models and I want to use the ObjectId to reference the user in the database however it does not work… I don’t know why.
I tried every conceivable syntax …i just bugs.
The ‘ref’ works fine when using a String datatype.
Any ideas on what’s wrong? The rest of my ExerciseTracker program works fine otherwise…
first model:
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var User = mongoose.Schema({
user: {
type: String,
required: true,
},
_id: ObjectId,
});
module.exports = mongoose.model('User', User);
second model:
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var exercise = mongoose.Schema({
userId: {
type: ObjectId,
ref: 'User'
},
description: String,
duration: Number,
date: {
type: Date,
default: Date.now,
}
});
module.exports = mongoose.model('Exercise', exercise);