2

I am creating a Node.js chat using socket.io
The problem is that when I see the console.log of history I see an array with A LOT of nulls and at the end my history entries [null,null,null......[ { username: 'Nobody Example', message: '231', date: '03/21/2013 14:23:58' } ]]

How come these nulls are in the array?
Here is a part of my code.

var history = [];

io.sockets.on('connection', function (socket) {

    socket.on('send', function (message) {
        var date = time();

        io.sockets.in(socket.room).emit('message', socket.username, message, date);

        history[socket.room].push({ username: socket.username, message: message, date: date });

        console.log(history);

    });

    socket.on('joinroom', function (room, username) {
        socket.room = room;
        socket.join(room);

        if ( typeof history[room] === 'undefined' )
            history[room] = [];

    });

});

Edit for more details:

The problem is in the 'joinroom' event when creating the empty array for each room.
Here are few tests that I've made:

socket.on('joinroom', function (room, username) {
    socket.room = room;
    socket.join(room);

    console.log(typeof history[room] == 'undefined');
    history[room] = [];
    console.log(typeof history[room] == 'undefined');
    console.log(JSON.stringify(history));
});

The console logs:

true
false
[null,null,null,null,..................,null,[]]
6
  • I think you want history to be an object instead of an array: var history = {}; Commented Mar 21, 2013 at 14:25
  • Then how am I going to add the history entry for each room? .push() is supported only by Array. Commented Mar 21, 2013 at 14:27
  • You're not pushing onto history directly, but on arrays in history, so that would work just fine. Commented Mar 21, 2013 at 14:29
  • When I change history = {} nothing really changes. The nulls are still there. I think there is something really messed up with node.js, because I test my code in the browser console (because it's just a simple work with arrays and objects) and there I get correct results (array without nulls). Commented Mar 21, 2013 at 14:48
  • Those nulls are a tell-tale sign of indexing an array with a large value: var a = []; a[23] = 1; console.log(JSON.stringify(a)); Commented Mar 21, 2013 at 14:50

2 Answers 2

6

If you have an empty array and index it with a large number (like your room id's), all slots in the array before that number are filled with undefined (which translates to null in JSON).

So try making history an object instead:

var history = {};
Sign up to request clarification or add additional context in comments.

1 Comment

Ooook, which translates to null in JSON, that is what led me here. Thanks.
0

try below code changing to object(hash);

var history = {};

io.sockets.on('connection', function (socket) {

socket.on('send', function (message) {
    var date = time();

    io.sockets.in(socket.room).emit('message', socket.username, message, date);

    if(history[socket.room] !== undefined) {
          history[socket.room].push = { username: socket.username, message: message, date: date   };
    } else {
        history[socket.room] = [{ username: socket.username, message: message, date: date   }];
    }

    console.log(history);

});

1 Comment

This is wrong, because it will keep only the last entry for each room. history[socket.room] = has to be array.

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.