should be const {Schema}= require("mongoose")
or “require” the whole “default export” from mongoose as const mongoose = require("mongoose") then you can use the mongoose object properties such as mongoose.Schema, mongoose.connect , mongoose.model etc
@nightmares Did you look at the docs as suggested in the challenge? Unlike the challenge, it has pretty clear examples of creating the schema and model right on the first page.
From the docs, I shortened the schema for simplification.
import mongoose from 'mongoose';
const { Schema } = mongoose;
const blogSchema = new Schema({
title: String, // String is shorthand for {type: String}
author: String,
body: String
});
const Blog = mongoose.model('Blog', blogSchema);
@lasjorg okey thank you have not considered/seen/used the other syntax before but it is good I have learned it. On the other hand:
@nightmares
A model is derived from a Schema and from the look of your code there is no Schema definition
A schema definition may look like:
const blogSchema = mongoose.Schema({
title:String, //or :point_down: this way
body:{
type: String,
required:true,
}, //or :point_down: this way
author:{
type: ObjectId,
require:[true,"A post must have an author"],
default: some ObjectId value,
}
})
then from the Schema you can create a model as:
const Blog = mongoose,model("Blog", BlogSchema)
the Blog object (model) now can be used to create new blogs.