Reputation: 107
It says that cannot read property authorid
undefined in socket.broadcast line.
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);
Upvotes: 1
Views: 1516
Reputation: 5634
The problem is that the value of i
is rows.length
when using the socket.
You can fix this by creating a function to which you send the current row or send the index
and use 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);
Upvotes: 1