Code Monkey home page Code Monkey logo

Comments (3)

rayonx avatar rayonx commented on July 20, 2024

The problem seems to be originating from models/room.js in the getUsers function. The first loop tries to create an array of userId. For some reason, the first item in the users array got skipped in the second for loop.

from chat.io.

rayonx avatar rayonx commented on July 20, 2024

I managed to fix the bug by ignoring the order of the users assigned to the users array, the reason is mainly that User.findById is an asynchronous call to mongoDB and if one of the previous user was not returned before the last one does, the callback is returned.

var getUsers = function(room, socket, callback){

	var users = [], vis = {}, curr = 0, count = 0;
	var userId = socket.request.session.passport.user;

	// Loop on room's connections, Then:
	room.connections.forEach(function(conn){
			// 1. Count the number of connections of the current user(using one or more sockets) to the passed room.
			if(conn.userId === userId){
				curr++;
			}
			// 2. Create an array(i.e. users) contains unique users' ids
			if(!vis[conn.userId]){
				User.findById(conn.userId, function(err, user){
					user.password = null;
					users.push(user);
					if (count === room.connections.length){
						return callback(null, users, curr);
					}
				});
			}
			vis[conn.userId] = true;
			count++;
	});
}

from chat.io.

OmarElgabry avatar OmarElgabry commented on July 20, 2024

I updated the code as investigated by @rayonx . Hope this will solve the problem.

from chat.io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.