I am writing a chat on NodeJS
and Socket.IO
and displaying messages in json on the page http://127.0.0.1/messages
I make a request for messages from the Mysql database and get:
id
(message ID),user_id
(ID of the message author),msg
(message text)After with the author's id, I get his avatar and name and shove everything into one object.
Code:
app.get('/messages', function(req, res) { var messages = {}; db.getConnection(function(err, connection) { connection.query('SELECT * FROM `chat` LIMIT 0, 30', function(err, rows) { res.setHeader("Access-Control-Allow-Origin", "*"); for (var prop in rows) { connection.query('SELECT user_name, user_photo FROM `users` WHERE user_id = "'+rows[prop]['user_id']+'"', function (error, result) { messages[rows[prop]['id']] = {user_id: rows[prop]['user_id'], msg: rows[prop]['msg'], info: result}; }); } res.send(JSON.stringify(messages)); connection.release(); }); }); });
But in object messages
nothing is written and I display on page {}