Mongoose import from file "buffering timed out"

Viewed 21

I'm trying to import some .json data to mongo, but when trying to save() a person, it just waits some seconds and then crashes with an error: MongooseError: Operation people.insertOne() buffering timed out after 10000ms It's weird, because when I'm doing POST request it works fine, the person is added correctly. I wrote some console.logs for help, and it prints the person correctly (inside for loop).

My import script file:

const fs = require('fs/promises')
const express = require('express')
const Person = require('../models/people')
const router = express.Router()

const importData = async () => {
    const data = await fs.readFile('./people.json')
    console.log(data)
    const dataParsed = JSON.parse(data)
    for(const person of dataParsed) {
        const personToSave = new Person({
            firstName: person.firstName,
            lastName: person.lastName
        })
        console.log(personToSave)
        await personToSave.save()
    }
}
importData()

module.exports=router

My connect:

const express = require('express');
const app = express();
const mongoose = require('mongoose');
const people = require('./routers/people');
const pets = require('./routers/pets');
const aggregate = require('./routers/aggregate');
const port = 3000;

app.use(express.json());
app.use('/people', people);
app.use('/pets', pets);
app.use('/aggregate', aggregate);

mongoose.connect('mongodb://localhost:27017/people').then(() => {
  console.log('Connected to mongoDB');
  app.listen(port, () => {
    console.log(`App is listening at port ${port}`);
  });
});
0 Answers
Related