JS Node returning only numbers and not proper values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



JS Node returning only numbers and not proper values



I'm trying to use js node to display a mysql query results so I can later output it to a webpage.



At the moment, I'm just outputting the resulsts of a query into the console, the code is below:


var mysql = require('mysql');
const querystring = require('querystring')

var connection = mysql.createConnection(
host : 'localhost',
user : 'webuser',
password: 'p0ssw0rd',
database: 'balancesheet2'
);

connection.connect(function(err)
if (err) throw err;
console.log('Connected!');
connection.query('SELECT * FROM balancesheet2.orders', function (err, result, fields)
if (err) throw err;
result = querystring.stringify(result,' );

);



The results i'm getting are just as below:


Result: 0 := | 1 := | 2 := | 3 :=



I'm not sure where the problem lies, is it with the code, or is it with the database somewhere?





Calling querystring.stringify on the results array seems a fairly odd thing to do. What result are you expecting from it?
– T.J. Crowder
Aug 10 at 11:06


querystring.stringify





Just do console.log(result), and leave out the querystring.stringify.. Node.js console.log isn't the best at displaying object properties and even worse if you prepend it with a string, for better output you might also consider util.inspect, -> nodejs.org/api/util.html#util_util_inspect_object_options
– Keith
Aug 10 at 11:22



console.log(result)


querystring.stringify





@Keith I'm not sure how or why it worked, but last time I tried that it just gave [object, object], I'm not sure if it was an access permission I changed or what.
– Peter Collins
Aug 10 at 11:32





If you just do -> console.log(result), it will work ok,.. But if you do console.log("something:" + result) it will not work ok.. That's because Object.toString() returns [object Object].
– Keith
Aug 10 at 11:34


console.log(result)


console.log("something:" + result)


[object Object]





@Keith +1 It was that simple Thanks
– Peter Collins
Aug 10 at 11:55





1 Answer
1



Your result is a array so you probably need to loop over to the content of result to get the values printed otherwise the existing code is simply printing the index of the array. Also note that the querystring.stringify() method produces a URL query string from a given obj by iterating through the object's "own properties".


result


result


querystring.stringify()





It's an odd thing to do, but querystring.stringify will loop through the result, which is why the OP is getting the numbers (0, 1, etc.) which are the own properties of the array. So the question is why the values are all blank.
– T.J. Crowder
Aug 10 at 11:06



querystring.stringify


result


0


1





@T.J.Crowder yes, it does that to make this happen OP needs to loop over result and do querystring.stringify() for each of that object.
– Ankit Agarwal
Aug 10 at 11:06


result


querystring.stringify()





Well, maybe. It depends on what he/she actually wants to achieve.
– T.J. Crowder
Aug 10 at 11:07





When I first ran the script without stringify, the results were all [object, object]. So I looked into solutions to that and found querystring.stringify(). I'm not sure what loop I would use or how I'd go about it
– Peter Collins
Aug 10 at 11:14






@PeterCollins what do you want to achieve? What's your expected output?
– Ankit Agarwal
Aug 10 at 11:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS