Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
Nextjs connect with MongoDB

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>
        </>
    )
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

That error happens if you do not have this line in your mongoose model file.

export default mongoose.models.CountDown || mongoose.model("CountDown", countdownSchema);

You should write your model in a separate file and then export it like above. If mongoose.models.CountDown is missing you get that error in next.js

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda