0

I am stumped.

I have successfully retrieved an array of objects from a mySQL database using NodeJS:

console.log('The clientData array: \n',clientData); returns:

Array (2)
    0: RowDataPacket {clientID: 1, nfpStatus: 0, companyName: "Enron"}
    1: RowDataPacket {clientID: 2, nfpStatus: 0, companyName: "Goldman Sachs"}

From the same scope as this console log call, I've tried to use several methods to walk through the array, but always it returns nothing. Zero. Zip. Zilch. There is no result, and there is no error.

Here are some of those methods:

$(clientData).each(function(index,values){ // what I would normally do
    console.log(index,values);
});

$.each(clientData, function(index,values){
    console.log(index,values);
});

clientData.forEach(function (index,values) {
    console.log(index,values);
});

clientData.forEach((index,values)=>{ // getting desperate now...
    console.log(index,values);
});

Any thoughts? Let me know if you require more information.


Here is the original MySql query, requested by Adam:

var clientData = Array(); // make global
connection.connect((err) => {
    if(err) throw err;
    connection.query('SELECT * FROM clients JOIN address ON clients.clientID = address.clientID', (err, rows, fields) => {
        if(err) throw err;
        $.each(rows, function(index, values){
            clientData.push(values);
        });
        connection.end();
    });
});
// my console.log call and foreach both fall in the global scope as well
Parapluie
  • 714
  • 1
  • 7
  • 22
  • `clientData` is an array, and `$(...)` is trying to wrap an HTML Node with jQuery's witchcraft. just use the built-in `.forEach` function on the array, e.g. `clientData.forEach(...)` and let me know if that helps. Otherwise I don't see a reason why your code shouldn't work – Jhecht Oct 02 '20 at 17:43
  • What are you trying to achieve? Can you show the rest of the code where you fetch the rows? – Adam Jenkins Oct 02 '20 at 17:44
  • @Adam I've included the fetch request above. Thanks. – Parapluie Oct 02 '20 at 17:55
  • @Parapluie - yeah, just what I thought, this is a dupe - I'll reopen it if you can show me where you're looping over `clientData` in the same snippet that you added in your edit and it turns out I was wrong. – Adam Jenkins Oct 02 '20 at 17:57
  • @Adam, so **keep it in the `connect` function, and all is good?** I suppose I could redesign the code so it needs not wait for the data. It would remain unusable until the data returns, but it wouldn't technically be broken. – Parapluie Oct 02 '20 at 18:08
  • @Parapluie - learn how to write async code - it's not nearly as hard as you think once you try. It just means "doing your work" in callbacks, and if you use `async/await` it doesn't even look like that, but I recommend the promises/callbacks first, otherwise you won't really understand what `async/await` is doing. – Adam Jenkins Oct 02 '20 at 18:24
  • 1
    @Adam Good advice. I touch javascript maybe once every two years — it's easy to get out of the headspace. Consider that the page you linked for me was already in my favourites! But without knowing the nature of my problem, I didn't know I should refer to it. Thank you. – Parapluie Oct 02 '20 at 18:31

0 Answers0