我有PHP网站,聊天模块工作正常(通过ajax)。 现在我正在为这个聊天模块创建Ionic应用程序,所以我试图在NODE.js上转换我的PHP代码
要获取聊天用户列表,我在helper.js文件中编写了此代码
setMessage:function(message1){
message=message1;
},
setPeople:function(id,people1){
people[id]=people1;
},
setChatlist:function(data9){
chatlist.push(data9);
},
setRecorder:function(data8){
recorder.push(data8);
},
getUserChatList:function(uid,connection,callback){
chatlist=[];
recorder=[];
people=[];
message={};
var data={
query:"select id from messages where recipient_id='"+uid+"' or timeline_id='"+uid+"' order by timestamp desc ",
connection:connection
}
var db_conncetion=data.connection;
var query=data.query;
var insert_data=data.insert_data;
db_conncetion.getConnection(function(err,con){
if(err){
con.release();
}else{
con.query(String(query),insert_data,function(err,rows){
if(!err) {
if(rows.length>0){
query1="select DISTINCT recipient_id from messages where timeline_id='"+uid+"' order by timestamp desc ";
con.query(String(query1),insert_data,function(err1,rows1){
rows1.forEach(function(element1, index1, array1){
var receiverId=element1.recipient_id;
var receiverSql = "SELECT time,text FROM messages WHERE timeline_id=" +uid+ " AND recipient_id=" +receiverId+" ORDER BY time DESC";
con.query(String(receiverSql),insert_data,function(err2,rows2){
rows2.forEach(function(element2, index2, array2){
people[receiverId]=element2.time;
message[receiverId]=element2.text;
});
self.setMessage(message);
self.setPeople(people,people);
});
});
});
var senderIdSql = "SELECT DISTINCT timeline_id FROM messages WHERE recipient_id=" +uid+ " ORDER BY time DESC";
con.query(String(senderIdSql),insert_data,function(err3,rows3){
rows3.forEach(function(element1, index1, array1){
var senderId=element1.timeline_id;
var senderSql = "SELECT time,text FROM messages WHERE recipient_id=" +uid+ " AND timeline_id=" +senderId+ " ORDER BY time DESC";
con.query(String(senderSql),insert_data,function(err2,rows2){
rows2.forEach(function(element2, index2, array2){
if (!self.isset(people[senderId]) || element2.time > people[senderId])
people[senderId]=element2.time;
message[senderId]=element2.text;
self.setPeople(senderId,element2.time);
self.setMessage(message);
});
});
});
});
}
Object.keys(people).forEach(function (key){
var tempArray={};
tempArray['id']=key;
tempArray['text']=message[key];
self.setRecorder(tempArray);
});
recorder.forEach(function (key){
var user_query="SELECT * FROM accounts WHERE id=" +key +" AND active=1 ";
con.query(String(user_query),insert_data,function(err3,rows3){
var tmp1=[];
rows3.forEach(function(element2, index2, array2){
var data1={};
data1['receiver_id']=element2.id;
data1['receiver_username']=element2.username;
data1['receiver_name']=element2.name;
data1['receiver_thumbnail_url']=element2.thumbnail_url;
data1['receiver_online']=0;
data1['receiver_message'] = recorder[key];
tmp1.push(data1);
});
self.setChatlist(tmp1);
});
});
var ret_data={};
ret_data['status']=200;
ret_data['success']=1;
ret_data['messages']=chatlist;
console.log(ret_data);
console.log("1111");
callback(ret_data);
con.release();
//console.log(message);
} else {
console.log(err);
console.log("Query failed");
}
});
}
});
}
但是当我通过套接字调用此方法时,它会给出一个空数组
在http://localhost:3000中收听 {状态:200,成功:1,消息:[]} 1111
请说明为什么它会给出一个空阵列?