1

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);
1
  • What does the console.log outputs? Commented Feb 7, 2015 at 17:52

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. It worked. But I can't seem to understand why i is being used by socket?
i was used by socket when you were calling socket.broadcast.to(rows[i].authorid). The value of i was higher than the length of rows so there was no element in rows[i]
Oh is it because the query is asynchronous?
For example having a list with 3 elements. rows[author_1, author_2, author_3]. You use this list in a for loop. You might think that when you first call socket.broadcast.to.. the value of i is 0 and you are using rows[0] => meaning author_1. Well this is wrong. You are actually accessing rows[3] which doesn't exist. And you already know the reason => asynchronous calls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.