Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:45:55+00:00 2026-06-12T17:45:55+00:00

First of all, this is the structure. { isActive : true, playerLimit : 2,

  • 0

First of all, this is the structure.

{ "isActive" : true,
  "playerLimit" : 2,
  "mode" : "Maul",
  "map" : "Dark woods",
  "name" : "Come and play with me",
  "_id" : ObjectId( "506fde5d7066645b8c000001" ),
  "players" : [ 
    { "hero" : "Brock, the summoner",
      "state" : "in lobby",
      "name" : "Elvar" }, 
    { "hero" : "Neal, the demon",
      "state" : "in lobby",
      "name" : "Test Spiller" } ],
  "__v" : 0 
}

I have selected just that with,

var game = db.model('Game');
game.find({_id : new ObjectID(data.game)}, function(err, res) {

Which works great. Inside the function however i want to traverse the players array, i’ve tried this

 res.players.forEach(function(player) {
    console.log(player.name +' | '+ data.name);
    if(player.name == data.name) permitted = true;
  });

Which seems not to work, i guess it because i a mongoCursor object, of some kind.

Since i’m using mongoose, i can’t use toArray(), i feel a bit lost, how can i traverse my Players array easely?

whole code

var game = db.model('Game');
game.find({_id : new ObjectID(data.game)}, function(err, res) {

  if(err) {
    // game not found, close connection.
    socket.emit('disconnected', 'Error: game was not found.')
    socket.disconnect();
  }

  var permitted = false;
  res.players.forEach(function(player) {
    if(player.name == data.name) permitted = true;
  });

  if(!permitted) {
    // game not found, close connection.
    socket.emit('disconnected', 'Error: you are not permitted, to enter this game.');
    socket.disconnect();
  }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:45:56+00:00Added an answer on June 12, 2026 at 5:45 pm

    The second parameter to your find callback (res in your example) with Mongoose is the array of documents that were found. Use findOne or findById when you’re only expecting a single match (like in this case). So try and change your code to something like this:

    var game = db.model('Game');
    game.findById(data.game, function(err, game) {
    
      if(err || !game) {
        // game not found, close connection.
        socket.emit('disconnected', 'Error: game was not found.')
        socket.disconnect();
        return;
      }
    
      var permitted = false;
      game.players.forEach(function(player) {
        if(player.name == data.name) permitted = true;
      });
    
      if(!permitted) {
        // game not found, close connection.
        socket.emit('disconnected', 'Error: you are not permitted, to enter this game.');
        socket.disconnect();
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe it's a similar beginning, but it's true. first of all sorry if this
First of all this is my markup: <div class=first></div> <div class=second></div> <div class=second></div> <div
first of all: this is not the same as this . The ModelBackend has
first of all this is my third question about web services here and i
First of all: This question is not directly programming related. However, the problem only
Well, first of all sorry about this question it must be pretty straight forward
First of all there is probably a question like this already but i couldn't
First of all I'm not sure this is even possible, however I need to
First of all I don't know if this is the right approach. I want
First of all I did gave a look at this one. But I didn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.