The function .save() in Mongoose is limited to executing only five times

While working with my House data seeder, I encountered a strange issue. Even though my code loops through the end, it only saves 5 records in the month model.

    const insertMonths = houses.map((house, i) => {
      const months = new Month({ year: "2021" });
      months.save();
      console.log(i);
      return { ...house, months: months._id };
    });
    await House.insertMany(insertMonths);

I'm puzzled by what might be causing this unexpected behavior.

Answer №1

I implemented the Promise.all method in my code

    const insertMonths = await Promise.all(
      houses.map(async (house) => {
        const createdHouse = await House.create(house);
        const createdMonth = await Month.create({ year: "2021" });
        const updateHouse = await House.findById(createdHouse);
        updateHouse.yearlyDues.push(createdMonth._id);
        await updateHouse.save();
        const updateMonth = await Month.findById(createdMonth);
        updateMonth.house = createdHouse._id;
        await updateMonth.save();
      })
    );    

Although I'm still learning about it, using Promise.all allows you to delve into Promises and extract specific values or perform certain actions. Feel free to correct me if I'm mistaken.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating a fixed header title when creating a customizable table

I am working on creating a dynamic table with rows and columns based on JSON data. JSON: $scope.dataToShow=[ tableHeder=["First Name","Age"], { name:"rahim", age:23 }, ...

Encountered an issue while attempting to send a POST request using AngularJS $

I am facing an issue with accessing the POST method from my server. Whenever I log the response, it always returns status=0. Can anyone help me out or provide some advice? Note: I have tested the method in Postman and it works fine. Below is the code snip ...

Best practice for sending a PDF file in JSON format with Node.js

Our team is currently in the process of building a Node.js API with Express.js. One specific function within our project involves receiving an ID and returning a pdf file from AWS in a json response format like this: { ID: "id10", pdf: [ ...

Changing the color of the timePicker clock in material-ui: a step-by-step guide

I have been attempting to update the color of the time clock in my timeInput component (material-ui-time-picker) for material-ui, but unfortunately, it is not reflecting the change. Here is the code I am using: <TimeInput style ={heure} ...

DOM doesn't reflect changes to nested object when component prop is updated

I have a complex object structured in a multidimensional way that appears as follows: obj: { 1: { 'a' => [ [] ], 'b' => [ [] ] }, 2: { 'x' => [ [], [] ] } } This object is located in the r ...

Limit the width and height of MUI Popper with a maximum setting

After experimenting with the popper API from MUI, I discovered that it extends beyond my main div. Does anyone have suggestions on how to prevent this overflow? I am looking to increase the height of the popper. Please refer to the code snippet below: con ...

Dependencies in AngularJS factories

I'm currently using AngularJS to extract data from mongodb. I have been attempting to utilize a factory to retrieve this information through the $http method. Despite researching and trying numerous approaches, none seem to work for me. In addition t ...

Does the AngularJS Controller disappear when the DOM element is "deleted"?

One challenge I encountered is regarding a directive that is connected to an angularjs controller, as shown below: <my-directive id="my-unique-directive" ng-controller="MyController"></my-directive> In the controller, at a certain point, I ne ...

Retain user IP address following Pipe usage in Express

My user IP address is getting lost after a pipe redirection. Before the redirection on the first express server: app.use('/api', function(req, res) { var url = 'http://localhost:4000/' console.log('ip Before:', req.ip); ...

Is it possible to operate a jQuery mobile web application while disconnected?

Recently, I've been experimenting with the idea of creating a web application using jQuery Mobile that involves utilizing forms such as checkboxes, text fields, and combo boxes. The tasks associated with this app are quite simple, but they require dat ...

Using PHP variables in JavaScript fetched through AJAX loading

I am currently attempting to retrieve a PHP variable from another page using AJAX in JavaScript, but it is not displaying any alerts. This is the PHP code for 'getposY': <?php include"connectdatabase.php"; $posYquery=mysql_query("Select posY ...

Querying Parse Server for objectId information

Within my web app that utilizes the Parse Server Javascript SDK, I have implemented the following query. While the console log accurately displays the retrieved information, the objectId field appears as "undefined." var query = new Parse.Query("myClass") ...

The For loop with varying lengths that exclusively produces small numbers

I'm currently using a for loop that iterates a random number of times: for(var i = 0; i<Math.floor(Math.random()*100); i++){ var num = i } This method seems to be skewed towards producing lower numbers. After running it multiple times, the &apo ...

javascript string assignment

Is it possible to conditionally assign a string based on the result of a certain condition, but for some reason, it's not working? var message = ""; if (true) { message += "true"; } else { message += "false" } console.log(message); ...

foreverjs neglects to log the child process's console.log output to any log files

I currently have a nodejs server running that fetches data using the setInterval function every x seconds. Here is a snippet of that part of the app: startPolling () { debug('Snmp poller started'); timers.setInterval( this.poll( ...

Is it possible to implement the EJS templating engine in nodeJS/express without utilizing the `res.render` function?

Is there a way for me to send a JSON object that includes an HTML snippet created from one of my EJS templates using res.send? res.send({ status: "xyz", timestamp: new Date(), htmlContent: "" //=====> HTML snippet from template.ejs here }); ...

Arranging arrays of various types in typescript

I need help sorting parameters in my TypeScript model. Here is a snippet of my model: export class DataModel { ID: String point1: Point point2 : Point point3: Point AnotherPoint1: AnotherPoint[] AnotherPoint2: AnotherPoint[] AnotherPoi ...

Implementing form validations using JavaScript for a form that is created dynamically

I have dynamically created a form using javascript and now I need to add mandatory validations on the form. The validations should trigger when the dynamically created button is clicked. However, I am facing an issue where I receive an error whenever I t ...

Struggling three.js newcomer faced with initial hurdle: "Function is undefined"

I am encountering a similar issue to the one discussed in this question: Three.js - Uncaught TypeError: undefined is not a function -- and unfortunately, the solutions provided there did not work for me. My journey with three.js began on the Getting Start ...

unable to access app.local in a routing file

So, in my express.js 4.13.3, I have set a variable called app.local in the app.js file. app.set('multimedia', __dirname + '/public/multimedia'); Now, in the routes/settings.js, I'm trying to access that variable like this: var a ...