Hello, all the passed tests were considered. The only remaining exercise is the “Blog Posts and Comments Modeling” exercise, which fails the “Is there a reference between posts and comments schema” test. This is my solution code:
I have passed the mongodb uri in this format (replacing the username, password and cluster address with the respective data from my mongo database):
const uri=“mongodb+srv://username:password@cluster-address/test?retryWrites=true&w=majority”
const postSchema = new mongoose.Schema({
title: String,
content: String,
author: String,
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment",
},
],
});
// Comment Schema
const commentSchema = new mongoose.Schema({
text: String,
author: String,
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
},
});
// Models
const Post = mongoose.model("Post", postSchema);
const Comment = mongoose.model("Comment", commentSchema);
Afterwards I tried to use the solution code provided on the platform and it doesn’t work either:
// Comment Schema
const commentSchema = new mongoose.Schema({
text: {
type: String,
required: true,
},
author: {
type: String,
required: true,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});
// Post Schema
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
},
content: {
type: String,
required: true,
},
author: {
type: String,
required: true,
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment",
},
],
createdAt: {
type: Date,
default: Date.now,
},
});
// Models
const Comment = mongoose.model("Comment", commentSchema);
const Post = mongoose.model("Post", postSchema);
Something is happening because the relationship is displayed correctly in the console: