Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Problema con la escritura en la base de datos que ocurre solo cuando el modelo se importa desde otro archivo

Guardar el modelo en la base de datos solo funciona si el modelo (mangosta) está en el mismo archivo, no entiendo por qué. El error que se da al importar un modelo de otro archivo es el siguiente:

MongooseError: el almacenamiento en búfer de Operation users.insertOne() se agotó después de 10000ms en Timeout. (/var/www/bhp_2/server/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:149:23)

  • modo: v16.13.0
  • mongodb: 4.1.4
  • mangosta: 6.0.12

conexión.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))

usuarios.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 answers
Answer question

0

Me las arreglé para arreglar el código para que el modelo funcione. En el archivo users.ts, en lugar de volver a importar el objeto mangosta ( import mongoose from "mongoose" ), usé la variable mangosta que usé para conectarme a la base de datos. Moví mongoose.connect a otro archivo y agregué una exportación de esta variable

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!