On Jul 15, 2016, at 10:28 PM, Ryan Schmidt wrote: > > On Jul 15, 2016, at 2:53 AM, paolodocet wrote: > >> I have an HTTP get request to extract from MySQL all records of a User >> table. >> >> User table looks like as follow: >> >> + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - >> - ID + Username + Email + Password + >> + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - >> - - - - >> - >> + 1 + a + a...@live.it + 123456789 >> + >> - - - - >> - >> + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - >> - - - - >> - >> + 2 + b + b...@live.it + 123456789 >> + >> - - - - >> - >> + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - >> - - - - >> - >> + 3 + c + c...@live.it + 123456789 >> + >> - - - - >> - >> + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - >> >> Then I create a javascript array where each element is a record of the >> previous table and using stringify I convert the js array in a JSON. My get >> response must reply with this JSON: >> >> app.get('/getUsers', function (req, res) { >> >> var users= []; >> var userJSON=""; >> >> >> connection.query('SELECT * FROM USER ORDER BY ID', function (err, rows, >> fields) { >> >> if (err) { >> console.log('There was an error\n' + err); >> } >> else { >> for (var i in rows) { >> users.push({ID:rows[i].ID, Username:rows[i].Username, >> Email:rows[i].email, Password: rows[i].Password}); >> } >> userToJson = JSON.stringify(users); >> console.log(userToJson); >> } >> }); >> res.setHeader('Content-Type','application/json'); >> res.json(userJSON); >> res.end(); >> >> }); >> >> My res.json(userJSON) does not work and my response is empty. How can I send >> to my client the JSON properly? > > You're calling res.setHeader, res.json and res.end *before* the userJSON > variable has been filled with data. userJSON is not being filled with data > until the anonymous function you give to connection.query runs, which won't > be until the database has had time to process the query. You need to call > your res functions in that anonymous function. > > > > app.get('/getUsers', function (req, res) { > > var users= []; > var userJSON=""; > > > connection.query('SELECT * FROM USER ORDER BY ID', function (err, rows, > fields) { > res.setHeader('Content-Type','application/json'); > if (err) { > console.log('There was an error\n' + err); > res.json({error: err}); > } > else { > for (var i in rows) { > users.push({ID:rows[i].ID, Username:rows[i].Username, > Email:rows[i].email, Password: rows[i].Password}); > } > userToJson = JSON.stringify(users); > console.log(userToJson); > res.json(userJSON); > } > res.end(); > }); > > });
I was focused on the flow of your code and not on variable names. I realize now that you also have variable name confusion: you have a variable userJSON which you declare and initialize to the empty string and never modify, and then you have a second variable userToJson which you fill with data but only use in your console.log. Let's fix those issues by using only a single variable: app.get('/getUsers', function (req, res) { connection.query('SELECT * FROM USER ORDER BY ID', function (err, rows, fields) { res.setHeader('Content-Type','application/json'); if (err) { console.log('There was an error\n' + err); res.json({error: err}); } else { var users = []; for (var i in rows) { users.push({ID:rows[i].ID, Username:rows[i].Username, Email:rows[i].email, Password: rows[i].Password}); } var userJSON = JSON.stringify(users); console.log(userJSON); res.json(userJSON); } res.end(); }); }); -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. To post to this group, send email to nodejs@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/E76CE480-C1CC-4CE5-89B0-EBA0700CFD4D%40ryandesign.com. For more options, visit https://groups.google.com/d/optout.