Sails.js 当模型之间存在多对多关系时,如何进行查询?

Sails.js 当模型之间存在多对多关系时,如何进行查询?,sails.js,Sails.js,我想做一个游戏。我需要创建一个匹配。我认为问题就在这方面。用户创建一个匹配项。在第三个表中,我保存了playerId和gameId。当另一个用户加入比赛时,我会再次保存playerId和gameId。然后,我与gameId相同的玩家进行查询,然后开始游戏 首先,一个用户可能有许多游戏。第二,一场比赛可能有很多比赛。这是匹配模型: module.exports = { attributes: { name: { type: 'string' },

我想做一个游戏。我需要创建一个匹配。我认为问题就在这方面。用户创建一个匹配项。在第三个表中,我保存了playerId和gameId。当另一个用户加入比赛时,我会再次保存playerId和gameId。然后,我与gameId相同的玩家进行查询,然后开始游戏

首先,一个用户可能有许多游戏。第二,一场比赛可能有很多比赛。这是匹配模型:

module.exports = {

  attributes: {

    name: {
        type: 'string'
    },

    description: {
        type: 'string'
    },

    game: {
        collection: 'game',
        via: 'gameId',
    }

  }
};
这是用户模型:

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();
        });
    }
};
这是游戏模型:

module.exports = {

  attributes: {

    gameId: {
        model: 'match'
    },

    playerId: {
        model: 'user'
    }



  }
};
最后,这是我的控制器:

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


};
我可以创建匹配,但Game.create发送此错误:

_http_outgoing.js:344抛出新错误('发送后无法设置头')^

错误:发送邮件后无法设置邮件头

有人能帮我吗?也许,我有很多错误。谢谢。

这里有几件事:

  • Sails中不需要明确的
    游戏
    模型。它可以隐式地管理它,除非您希望存储的信息不仅仅是
    gameId
    userId
    。因此,您可以取消
    游戏
    模型
  • 有关异步编程,请参阅:
  • 下面的代码应该适合您。希望能有帮助

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

    谢谢你,朋友,它工作得很好。为什么要存储比gameId和userId更多的信息?您需要更多的表吗?除此之外,不需要使用联接表来存储匹配和用户之间的多对多关系。如果要存储的信息只有
    matchId
    userId
    (表的
    id
    ),那么Sails在没有显式模型的情况下管理它。然而,若联接表中需要更多的属性,那个么Sails目前不支持它。所以在这两种情况下都有一个联接表,它只是显式声明它或隐式管理它的框架。许多关系: