Failed to transfer the result set from the database to an array in JavaScript

I've implemented a query to retrieve the list of new users from the database. The query is functioning correctly and returning a total of 15 users. However, when I attempt to store the resultset into a JavaScript array, only the last record is being saved.

Here is a snippet of my code:

var query = `SELECT * 
                FROM users
                WHERE (status ='New')`;
var query = connection.query(query),
    response = []; // This array will store the results of our database query
query
    .on('error', function (err) {
        console.log(err);
    })
    .on('result', function (res) {
        // We are populating our array by iterating through each user row in the database
        response.push(res);
        /*
        for (var key in res) {      
            if (res.hasOwnProperty(key)) response.push(res[key]);
        }
        */

    })
    .on('end', function () {
        console.log('console')
    });

The line response.push(res); seems to be causing the issue. I have also experimented with other methods, as shown in the commented lines below that particular line, but none seem to yield the desired outcome.

Answer №1

Consider implementing a for loop

for(let item in results){
    feedback.push(results[item]);
}

Answer №2

Perhaps I am underestimating your test, but it seems you may be checking the result in the incorrect location.

You should be doing this on the 'end' callback.

.on('end', function () {
    console.log(res)
});

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

Utilizing the Jquery ready method in conjunction with the load function

While utilizing the ready() method to access the DOM, I encountered an issue where jQuery was returning undefined when trying to access an element. Even after nesting the ready() function inside $(window).on("load", function()), there were still instances ...

What is the method for applying border-corner-radius exclusively to the left side of a button using jquery-ui-1.9.2.custom?

In my HTML code, I have the following: <div id="ot-lang-src"> <button id="rerun"></button> <button id="select">Choose a language</button> <ul id="ui-menu-left"> < ...

What steps can I take to improve the efficiency of this filtering process in Vue.js?

I am seeking ways to enhance the method I utilize for determining which values should be displayed in a table. At present, my table displays various values and is accompanied by input fields. The values chosen in these input fields serve as filters for the ...

What is the process for choosing a dropdown menu on a website created with JavaScript, specifically utilizing Selenium in Python3?

I need help with selecting the item "Short_Budget_Report" from a website's HTML code using Selenium and the Select module. Here is the relevant section of the HTML code: <input id="WD51" ct="CB" lsdata="{1:'20ex', ...

Attempting to set up an Ajax webform with various outputs, but encountering issues with functionality

I'm encountering an issue while creating interactive exercises. I want to display correct or incorrect answers immediately after submission using JSON for retrieving responses, as suggested in a forum. However, my AJAX code isn't working at all. ...

Sharing Axios Response Data in VueJS: A Guide for Parent-Child Component Communication

Can someone please help me with using VueJS and Axios to retrieve API data and pass it to multiple child components? I am trying to avoid accessing the API multiple times in the child components by passing the data through props. The issue I am facing is ...

innerHTML not showing up on the page

Trying to implement a dynamic navbar that changes based on whether a user is signed in or not. The authentication part is functioning correctly (verified with console logs); however, there seems to be an issue with updating the HTML using .innerHTML = ... ...

Obtain information in JSP from a JavaScript function that was generated in another JSP file

I have created a code that allows the admin to toggle a user's status between active and inactive using radio buttons. The technologies I have used for this project are JSP, MySQL, and JS. admin.jsp: <tr> <td> ...

When using the Ng --version command on a development package, it throws an error

I encounter an error with a development package when cloning a repository. I would greatly appreciate any advice on how to resolve this issue. https://i.stack.imgur.com/DBp5r.png ...

Exploring the possibilities of jQuery with Accordion functionality and creating dynamic multiple menus

Incorporating the Wayfinder and Accordion menus, I have set up a two-level menu structure for the left column. The structure looks like this: <ul class="accordion">: Menu 1 Sub-menu 1.1 Sub-menu 1.2 Sub-menu 1.3 Menu 2 Sub-menu 2 ...

Setting the vertices of a THREE JS geometry based on a specified angle

I have the desire to create a triangle with known angles (Alpha, Beta, Gamma) and side lengths (10). In order to draw a triangle, I must assign 3 vertices to the geometry with specific Vector3 values. Does THREE.js offer any tools or techniques suitable ...

Node.js utilizing modules as dependencies

Is there a recommended method for organizing module dependencies into a separate file named dependencies.js, which can then be required in server.js? How can I efficiently return all of these required modules? var express = require('express') ...

Refresh Vue/Nuxt Components Fully

Understanding how this.$forceUpdate() functions, I am not simply looking to re-render the component. In Nuxt applications, page components have asyncData() as a lifecycle method that runs before created(). I utilize this method to retrieve initial data an ...

Erasing a Cookie

I'm currently developing a feature on my website that involves adding [ITEM] and using cookies. The Add [ITEM] functionality is already working, but now I need to implement a Remove [ITEM] feature. Below is the code snippet I have so far: $(window).l ...

Is there a way to reverse the image without physically clicking on it?

I've created a JavaScript code for matching images in a 4x4 grid. The goal is to flip the images when clicked and then flip them back if they don't match. I've managed to flip the images initially, but there's an issue with flipping the ...

"Exploring the power of asynchronous operations in NodeJS using Q

Currently, I am utilizing Node.js and I aim to incorporate promises to ensure a complete response following a for loop. exports.getAlerts = function(req,res,next){ var detected_beacons = []; if (!req.body || Object.keys(req.body).length == 0) { res.s ...

Experience the simplistic magic of the Vue.js/Vuefire/Firebase app world, though it struggles with reading values

Transitioning to Vue.js from SQL programming has been a bit of a challenge, but I'm getting there. Currently, I am using vuefire according to the specifications and even manually inserting the correct key from my Firebase database with one record and ...

Highcharts displays data with the fourth y axis but doesn't include labels for it

I'm facing an issue with displaying all the labels on my chart. I have 4 series plotted and decided to add two y-axes on each side of the graph, but the labels for the last series named "Stuff Data" are not showing up correctly. Instead, it seems to b ...

Trigger the change-event by hand

Hey there, I'm facing an issue with manually triggering a change event. Currently, I have a selectOneMenu (similar to a dropdown in JSF) with various values. When I select a value from this dropdown list, a datatable is supposed to be updated. This ...

Disregard earlier callback outcome if there has been a change in the state since then

I am facing an issue with my page that displays a list of countries retrieved from an external library. When I click on a country, it should show me all the cities in that specific country. Each country object has a provided method to fetch the list of c ...