I'm new in nextjs and coding, i try in node.js is fine, but i try in next.js, i get this error, help!
Thank You,
OverwriteModelError: Cannot overwrite 'CountDown' model once compiled.
import mongoose from 'mongoose'
// mongoose connection
mongoose.connect(process.env.MONGODB_URI);
// mongoose items setup -------------------------
const countdownSchema = {
title: {
type: String,
required: [true, 'Please input Event name'],
unique: true,
trim: true,
maxLength: [50, 'Title cannot be more then 50 charactor']
},
year: String,
month: String,
day: String,
time: String
};
// mongoose model
const CountDown = mongoose.model("CountDown", countdownSchema);
// end mongoose setup ----------------------------
export default function Model() {
// default build mongoDB
const item = new CountDown({
title: '2023 Chinese New Year',
year: '2023',
month: 'January',
day: '22',
time: '09:00'
});
item.save();
return (
<>
<h1>Hello World</h1>
</>
)
}
CodePudding user response:
You cannot connect to your MongoDB from javascript apps, because javascript runs on the browser while your database resides on the server.
Instead, you should be thinking about coding an API using node, and then expose endpoints you can call from javascript side to execute MongoDB operations.
