带有mysql的nodejs无法查询两次

时间:2015-05-09 17:50:55

标签: mysql node.js

var messages = [];
var senders = [];
db.query('select * from message where (sender_id=? and receiver_id = ?) or (sender_id=? and receiver_id = ?) order by created desc limit 0,5 ', [senderId, receiverId, receiverId, senderId])
    .on('result', function (data) {
        messages.push(data);
    })
    .on('end', function () {
        db.query('select name,thumb_file_name,user_id,thumbnail_width,thumbnail_height from users where user_id = ? ', senderId)
        .on('result', function (data) {
            senders.push(data);
        })
        .on('end', function () {
            console.log('messages',messages);
            console.log('users',senders);
            console.log('senderId',senderId);
            console.log('receiverId',receiverId);
            io.to(socketId).emit("message", {messages: messages, users: senders});
        });
    });

在本地,这可以获取消息和用户,但是当它在服务器上时,它只查询一次,因此缺少“消息”。

0 个答案:

没有答案