i am trying to make a game. I need tu create a Match. I think the problem on this Way. The User create a Match. In a third table I save playerId and gameId. When another user join the match, I save again, playerId and gameId. Then, I make a query with player with gameId in common, and start the game.
first, One User may have many Games. second, One Match may have many Games. this is the Match model:
module.exports = {
  attributes: {
    name: {
        type: 'string'
    },
    description: {
        type: 'string'
    },
    game: {
        collection: 'game',
        via: 'gameId',
    }
  }
};
This is the User model:
var bcrypt = require('bcrypt');
module.exports = {
  attributes: {
    name: {
        type:'string'
    },
    email: {
        type: 'email',
        required: true,
        unique: true
    },
    password: {
        type: 'string',
    },
    passwordConfirmation: {
        type: 'string'
    },
    passwordEncrypted: {
        type: 'string'
    },
    creator: {
      collection: 'game',
      via: 'playerId'
    },
    toJSON: function(){
        var obj = this.toObject();
        delete obj.password;
        delete obj.passwordConfirmation;
        delete obj._csrf;
        return obj;
    }
  }, beforeCreate: function(values, next){
        console.log("Acabo de entrar a eforeCreate");
        var password = values.password;
        var passwordConfirmation = values.passwordConfirmation;
        if(!password || !passwordConfirmation || password != values.passwordConfirmation) {
            var passwordDoesNotMatchError = [{
            name: 'passwordDoesNotMatchError',
            message: 'Las contraseñas deben coincidir'
        }]
        return next({
            err: passwordDoesNotMatchError
            });
        }
        require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, EncryptedPassword){
            values.EncryptedPassword = EncryptedPassword;
            next();
        });
    }
};
This is the Game model:
module.exports = {
  attributes: {
    gameId: {
        model: 'match'
    },
    playerId: {
        model: 'user'
    }
  }
};
finally, this is my controller:
module.exports = {
createMatch: function(req,res){
        var matchObj = {
            name: req.param('name'),
            description: req.param('description'),
        }
        Match.create(matchObj, function(err, match){
            if(err){
                console.log("el error fue: " + err);
                return res.send(err);
                } console.log("Entro en create");
                 return res.json(match);
        })
        var gameObj = {
            gameId: 'aclaration: I dont know how do I get the match.id',
            playerId: req.session.me
        }
        Game.create(gameObj,function(err,game){
            console.log("entro a GameCreate");
            if(err){
                return res.send(err);
            } return res.json(game);
        })
    }
};
I can create the Match, but Game.create send this error:
_http_outgoing.js:344 throw new Error('Can\'t set headers after they are sent.'); ^
Error: Can't set headers after they are sent.
Somebody can help me? probably, I have many errors. Thanks.
 
     
    