i tried to create a document using
const user = new User({name:"henok", age:"23"})
user.save()
console.log(user)
and it works but when i try to create a new doucment using
it gives me an error
const run = async()=>{
try {
const user = await User.create({name:"someone", age:55})
console.log(user)
} catch (error) {
console.log(error.message)
}
}
the error
here is the mongoose version im using “mongoose”: “^8.3.2”
and here is the full code
import mongoose from "mongoose";
import User from "./script.js";
mongoose.connect("mongodb://localhost:27017/appdb")
const run = async()=>{
try {
const user = await User.create({name:"someone", age:55})
console.log(user)
} catch (error) {
console.log(error.message)
}
}
run()
script.js
import mongoose from "mongoose";
const userSchema = new mongoose.Schema({
name: String,
age: Number,
})
export default mongoose.model("User" ,userSchema)
New contributor
Henok is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.