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

103
Visualizações
Problem with writing to the database that occurs only when the model is imported from another file

Saving the model to the database only works if the model (mongoose) is in the same file, I don't understand why? The error that occurs when importing a model from another file is as follows:

MongooseError: Operation users.insertOne() buffering timed out after 10000ms at Timeout. (/var/www/bhp_2/server/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:149:23)

  • mode: v16.13.0
  • mongodb: 4.1.4
  • mongoose: 6.0.12

connection.ts

import { Mongoose } from "mongoose"

//I also tried with es6 import like above
const User = require('../models/users')    

const mongoose = new Mongoose()
const conectionOpt = {
    useNewUrlParser: true
}     

const mongoDbUri = process.env.MONGODB_URI   

mongoose.connect(mongoDbUri, conectionOpt).then(()=>{
  console.log("MongoDB database connection established successfully");

// if I uncomment the code snippet below and comment out the import model file at the top, then insert to the database will work without a problem
//
//   const userShema = new mongoose.Schema({
//   first_name: {
//       type: String,
//       required: true,
//       trim: true
//   },
//   last_name: {
//       type: String,
//       required: true,
//       trim: true
//   }
// })

// const User = mongoose.model('User', userShema)

  const user = new User({
    "first_name" : "Bob",
    "last_name": "Test"
  })

  user.save();
}).catch(err => console.log(err))

users.ts

import mongoose from "mongoose";

const userShema = new mongoose.Schema({
    first_name: {
        type: String,
        required: true,
        trim: true
    },
    last_name: {
        type: String,
        required: true,
        trim: true
    }
})

const User = mongoose.model('User', userShema)
module.exports = User
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I managed to fix the code to make the model saving work. In the users.ts file instead of re-importing the mongoose object (import mongoose from "mongoose") I used the mongoose variable which I used to connect to the database. I moved mongoose.connect to another file, and added an export of this variable

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