How to make a for loop iterate through the additional element added to array from db result?

How to make a for loop iterate through the additional element added to array from db result?

I have an array. I want to query a mysql database with each array element and on a certain condition fulfillment, I want to add additional elements to the same array and iterate through those elements as well.

I have an array. I want to query a mysql database with each array element and on a certain condition fulfillment, I want to add additional elements to the same array and iterate through those elements as well.

I'm new to nodejs so I don't know if this is the right approach.

I have provided a code snippet that is a simplified version of my issue.

In below code snippet, I want to display "looks good" on console.

pre_req_subjects_arr = [2310,2320,2410];
for(let j=0;j<pre_req_subjects_arr.length;j++) {
    console.log("j is : "+j);
    db.query("SELECT `NUMBER` FROM `COURSE`",function(err,preOfPreReq) {
        if(err) {
            console.log("Error while creating array of pre_req subjects : " + err);
        } else {
            console.log("j inside db query : "+j);
            if(j==1) {
                pre_req_subjects_arr.push(1111);    
            }
            if(pre_req_subjects_arr[j] == 1111) {
                console.log("looks good");
            }
        }
    })                                        
}

setTimeout(function() { console.log("pre_req_subjects_arr is " + JSON.stringify(pre_req_subjects_arr)); }, 1000);

Output:

j is : 0
j is : 1
j is : 2

j inside db query : 0 j inside db query : 1 j inside db query : 2

pre_req_subject_arr : [2310,2320,2410,1111]

Expected Output:

j is : 0
j is : 1
j is : 2
j is : 3

j inside db query : 0 j inside db query : 1 j inside db query : 2 j inside db query : 3

looks good
pre_req_subject_arr : [2310,2320,2410,1111]


javascript node-js database

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.