它表示无法在socket.broadcast行中读取未定义的属性authorid
。
setInterval(function(){
db.query("select * from `notifications`", function(err, rows){
if (rows.length>temp){
for (var i=temp; i<rows.length; i++){
console.log('sending room post', rows[i].authorid);
db.query("select `name` from `users` where `id`=?", [rows[i].authorid ], function(err, name){
name=name[0].name;
socket.broadcast.to(rows[i].authorid).emit('new notification', {
authorname:name,
dpid:rows[i].followid,
read:rows[i].read,
type:rows[i].type,
followstatus:1
});
});
}
temp=rows.length;
}
})
}, 3000);
答案 0 :(得分:1)
问题是使用套接字时i
的值为rows.length
。
您可以通过创建发送当前行的功能或发送index
并使用rows[index]
来解决此问题:
setInterval(function() {
db.query("select * from `notifications`", function(err, rows) {
// You access the values of the current row
// row.authorid
// ...
var broadcast = function(row) {
console.log('sending room post', row.authorid);
db.query("select `name` from `users` where `id`=?", [row.authorid], function(err, name) {
name=name[0].name;
socket.broadcast.to(row.authorid).emit('new notification', {
authorname: name,
dpid: row.followid,
read: row.read,
type: row.type,
followstatus: 1
});
});
};
if (rows.length>temp) {
for (var i=temp; i<rows.length; i++) {
broadcast(rows[i]);
}
temp=rows.length;
}
})
}, 3000);