2014-07-03 57 views
1

我遇到了两个模型在sails中的链接问题。我有两个模特'歌手'和'国家'。在'歌手'模式中,我有一个属性'singer_country',意思是'国家'模式的ID。我的问题是,当我显示所有歌手的属性时,我可以在'Country'模型中获得属性'country_name'。我不知道它是如何做到的。下面是我的代码: 我的 '歌手' 的模式Sails.js中两个模型的关联

module.exports = { 

attributes: { 
singer_name:{ 
    type: 'string', 
    required: true, 
    size: 50 
}, 
singer_realname:{ 
    type: 'string', 
    size: 50 
}, 
singer_gender:{ 
    type: 'string', 
    enum: ['Male', 'Female'], 
    defaultsTo: 'Male' 
}, 
singer_brithday:{ 
    type: 'int' 
}, 
singer_image:{ 
    type: 'string' 
}, 
singer_description:{ 
    type: 'string' 
}, 
singer_country:{ 
    type: 'string' 
} 

} 
}; 

我的 '国家' 的模式:

module.exports = { 

attributes: { 
country_name: { 
    type: 'string' 
} 
} 
}; 

我的方法来显示歌手的属性:

index: function(req, res, next){ 
    Singer.find().exec(function foundSinger(err, singerObj){ 
     if(err) return next(err); 
      res.view({ 
       singers: singerObj, 
     });  
    }); 
}, 

我的数据库MongoDB的,我正在使用sails beta 0.10 rc8。感谢您的帮助。

回答

1
singer_country:{ 
    model: 'Country' 
} 

Singer.find().populate('singer_country').exec(function foundSinger(err, singerObj){ 
    if(err) return next(err); 
     res.view({ 
      singers: singerObj, 
    });  
}); 
+0

它返回一个错误: “TypeError:Object [object Object]没有方法'填充''” – Tung