1

I want users to have the ability to click a button that pushes their username and id into an array associated with a collection in a database, but only if they're not already in that array.

My solution is:

var isInGroup = function(user, arr){

    var match = arr.indexOf(user);
    console.log(">>>>>>>" + match);
    if(match === -1){
        arr.push(user);
        console.log("added user");

    } else {
        console.log("Already in group");
    }
};

This works when I test it against example arrays in the console, but not when I'm querying the database. When I execute the function in my app, arr.indexOf = -1 even if the user is already in the array.

This is the relevant code:

Player.js

var express = require("express"),
    router = express.Router({mergeParams:true}),
    Game = require("../models/game"),
    Player = require("../models/player"),
    User = require("../models/user"),
    middleware = require("../middleware");


//Add A Player 
router.post("/", middleware.isLoggedIn, function(req, res){
    //find game
    Game.findById(req.body.game, function(err, foundGame){
        console.log(">>>>" + foundGame);
         if(err){
            req.flash("error", "Something went wrong.");  
      }  else {
          //create player
          Player.create(req.user, function(err, player){
              if(err){
                  console.log(">>>> error" + player);
                  res.redirect("back");
              } else {

                  player.id = req.user_id;
                  player.username = req.user.username;
                  middleware.isInGroup(player, foundGame.players);

                  foundGame.save();
                  res.redirect("back");
              }
          });
        }
    });
});

Game Schema

var mongoose = require("mongoose");

var gameSchema = new mongoose.Schema({
    name:String,
    author:{
        id:{
            type: mongoose.Schema.Types.ObjectId,
            ref:"User"
        },
        username:String,
    },
    court:{
        id:{
            type:mongoose.Schema.Types.ObjectId,
            ref:"Court"
        },
        name:String,
    },
    players:[
        {
            id:{    type:mongoose.Schema.Types.ObjectId,
            ref:"Player",
            },
            username:String
            }


    ],
    time:{
        start:String,
        end:String
    },
    date:String,
});

module.exports = mongoose.model("Game", gameSchema)

Player Schema

var mongoose = require("mongoose");

var playerSchema = new mongoose.Schema({

        id:{type:mongoose.Schema.Types.ObjectId,
        ref:"User" 
        },
        username: String

});

module.exports = mongoose.model("Player", playerSchema);

User Schema
var mongoose              = require("mongoose"),
    passportLocalMongoose = require("passport-local-mongoose");

var userSchema = new mongoose.Schema({
    username: String,
    password: String
});

userSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", userSchema);

As mentioned above, arr.indexOf(user) returns -1 even if user is already in the array. Why is this happening? Is there better solution to this problem? Thanks for the help. I've been banging my head for awhile on this one.

Josh
  • 11
  • 1
  • Possible duplicate of [Object comparison in JavaScript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – lilezek Jan 26 '18 at 21:42
  • Possible duplicate of [indexOf method in an object array?](https://stackoverflow.com/questions/8668174/indexof-method-in-an-object-array) – dvsoukup Jan 26 '18 at 21:45
  • Also note that because in JavaScript non-primitives are passed by reference, modifying the function input directly is generally not a good idea and against the priincple of a stateless (and thus easily testable) code. Your function should return a new object instead of manipulating arguments. – Michał Szydłowski Jan 26 '18 at 21:56

0 Answers0