我正试图找到一种方法来在我的团队模型中获取用户而不使用多对多。
我在Team.js中创建了一个方法调用getCaptain:
getCaptain: function(){
User.findOne(this.admins[0],function foundUser(err, user){
if(err) return "ERROR"
if(!user) return "No captain"
return user;
});
},
在团队中我有: “name”是primaryKey
{
"name": "abc",
"id": null,
"players": [
4
],
"admins": [
4
],
"createdAt": "2014-11-20T18:36:10.559Z",
"updatedAt": "2014-11-20T18:36:10.559Z"
}
如果我呼叫console.log(abc.getCaptain())
,它将返回未定义,即使用户4存在。
功能本身是否有任何问题,或者我不能在团队模型中寻找用户?
答案 0 :(得分:0)
我从未尝试过您正在使用的findOne的语法。你试过文档中的那个吗?
User.findOne({ id: 1 }, function(err, user) {
// Do stuff here
});
也许它不会自动将整数4识别为对id字段的查询。