Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

317
Vistas
Mongoose Work only first time but doesn't works second time onwards

Below is the Code:

import mongoose from "mongoose"
const testingScript = async () => {
    for (let i = 0; i < 10; i++) {
      console.log("Index", i);
      mongoose.connect('URL');
      
      var connection = mongoose.connection;
      
      connection.on('error', console.error.bind(console, 'connection error:'));
      connection.once('open', function () {
      
          connection.db.collection("test", function(err, collection){
              collection.find({abc: "12345"}).toArray(function(err, data){
                  console.log(data); // it will print your collection data
              })
          });
      
      });

    }
  };

  testingScript()

Below is code for Mongoose Schema that is saved in DB.

import { Schema, model, Document, Types, Mongoose } from "mongoose";

const userSchema = new Schema(
  {
    name: {type: String},
    email: {type: String},
    deletedAt: { type: Date, default: null },
    schema_version: { type: Number, default: 1 },
  },
  {
    timestamps: true,
  }
);


export interface IUser extends Document {
  _id: string;
  name: string;
  email: string;
  createdAt: Date;
  updatedAt?: Date;
  deletedAt?: Date;
  schema_version: number;
}
const user = model<IUser>("User", userSchema);

For the first POST request, it will get all the records but afterward, it will get only one record. Moreover after the second request, even GET doesn't return anything from DB.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda