Sailsjs Model.find() 不检索外键
Sailsjs Model.find() not retrieving foreign key
我在 sails v0.12 中有两个具有一对多关联的模型:一个游戏模型和一个用户模型(一个游戏有两个用户)。
当我找到一个用户时,我想查看其游戏对象的外键。记得以前在sails的一个版本里看到过这个属性,现在好像没有了。
这是我的模型的简化版本:
Game.js:
module.exports = {
name: {
type: 'string',
},
players: {
collection: 'user',
defaultsTo: []
}
};
和User.js:
module.exports = {
email: {
type: 'string',
required: true
},
game: {
model: 'game'
}
};
我想找到一个用户模型并查看其 'email' 和 'game' 属性。这是一个示例查询:
User.find(7).exec(function gotUser(user) {
console.log(user); // -> {'email': 'someUserEmail'} MISSING 'game'
}
编辑:
即使在 find():
上调用 populateAll() 时,'game' 属性也不会出现
User.find(7).populateAll().exec(function gotPopulatedUser(user) {
console.log(user); // -> {'email': 'someUserEmail'} MISSING 'game'
}
有没有办法在找到用户时看到用户关联的游戏ID(外键)?还是我需要 搜索在其玩家集合中具有用户 ID 的所有游戏?它的语法是什么?
这是我的错误!
Game 对象的模型定义缺少 'via' 键。
不知何故,两者已经充分关联,无需此即可填充游戏对象(game.find().populateAll() 将成功填充 'players' 属性),但游戏模型应为:
module.exports = {
name: {
type: 'string'
},
players: {
collection: 'user',
via: 'game', //This was missing
defaultsTo: []
}
};
有了这一行,User.find() 将产生一个将相关游戏的 ID 作为其 'game' 属性的用户,而 user.find.populateAll() 将产生一个正如预期的那样,将填充的游戏作为其 'game' 对象的用户。
我在 sails v0.12 中有两个具有一对多关联的模型:一个游戏模型和一个用户模型(一个游戏有两个用户)。
当我找到一个用户时,我想查看其游戏对象的外键。记得以前在sails的一个版本里看到过这个属性,现在好像没有了。
这是我的模型的简化版本:
Game.js:
module.exports = {
name: {
type: 'string',
},
players: {
collection: 'user',
defaultsTo: []
}
};
和User.js:
module.exports = {
email: {
type: 'string',
required: true
},
game: {
model: 'game'
}
};
我想找到一个用户模型并查看其 'email' 和 'game' 属性。这是一个示例查询:
User.find(7).exec(function gotUser(user) {
console.log(user); // -> {'email': 'someUserEmail'} MISSING 'game'
}
编辑: 即使在 find():
上调用 populateAll() 时,'game' 属性也不会出现User.find(7).populateAll().exec(function gotPopulatedUser(user) {
console.log(user); // -> {'email': 'someUserEmail'} MISSING 'game'
}
有没有办法在找到用户时看到用户关联的游戏ID(外键)?还是我需要 搜索在其玩家集合中具有用户 ID 的所有游戏?它的语法是什么?
这是我的错误!
Game 对象的模型定义缺少 'via' 键。
不知何故,两者已经充分关联,无需此即可填充游戏对象(game.find().populateAll() 将成功填充 'players' 属性),但游戏模型应为:
module.exports = {
name: {
type: 'string'
},
players: {
collection: 'user',
via: 'game', //This was missing
defaultsTo: []
}
};
有了这一行,User.find() 将产生一个将相关游戏的 ID 作为其 'game' 属性的用户,而 user.find.populateAll() 将产生一个正如预期的那样,将填充的游戏作为其 'game' 对象的用户。