1

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.

Francisco Possetto
  • 443
  • 1
  • 3
  • 10

1 Answers1

1

Couple of things here:

  1. Having an explicit Game model is not required in Sails. It can manage it implicitly, unless you want to store more information than just gameId and userId. So, you can just do away with Game model.
  2. Please refer for async programming: How do I return the response from an asynchronous call?

Below code should work for you. Hope it helps.

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");

      var gameObj = {
        gameId: 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);
        // return res.json(match);
      });
    });
  }
};
Community
  • 1
  • 1
Sangharsh
  • 2,999
  • 2
  • 15
  • 27
  • Thank you friend, its works perfectly. Why do you want to store more information than gameId and userId you need a more table? And otherwise it is not necessary – Francisco Possetto Jan 22 '17 at 21:45
  • A _join_ table is needed to store many to many relationship between Match and User. If only information to be stored is `matchId` and `userId` (an `id` of the table) then Sails manages it without having explicit model. However, if more attributes are needed in join table then Sails doesn't support it as of now. So a join table is there in both cases, it is just case of declaring it explicitly or framework managing it implicitly. Many-many relationship: http://sailsjs.com/documentation/concepts/models-and-orm/associations/many-to-many – Sangharsh Jan 23 '17 at 02:01