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(); }); }); -- 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/D2702B76-9021-4E68-84BA-74B2A220F2FF%40ryandesign.com. For more options, visit https://groups.google.com/d/optout.