Try this: 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 { rows.forEach(function( item){ users.push({ID:item.ID, Username:item.Username, Email:item.email, Password: item.Password}); }); userToJson = JSON.stringify(users); console.log(userToJson); } }); res.json(users); } Regards Marco > El 15/07/2016, a las 2:53 a.m., paolodocet <paolodo...@gmail.com> escribió: > > 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? > > -- > Job board: http://jobs.nodejs.org/ <http://jobs.nodejs.org/> > New group rules: > https://gist.github.com/othiym23/9886289#file-moderation-policy-md > <https://gist.github.com/othiym23/9886289#file-moderation-policy-md> > Old group rules: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > <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 > <mailto:nodejs+unsubscr...@googlegroups.com>. > To post to this group, send email to nodejs@googlegroups.com > <mailto:nodejs@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/nodejs/06e3ec94-2f99-4c34-9898-79349254a5cb%40googlegroups.com > > <https://groups.google.com/d/msgid/nodejs/06e3ec94-2f99-4c34-9898-79349254a5cb%40googlegroups.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- 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/319DA0DD-3458-4B9A-A705-E274DA9636E1%40gmail.com. For more options, visit https://groups.google.com/d/optout.