我遇到了.populate在Sails中工作的问题。如果我使用下面的模型,我使用.populateAll然后它工作,我得到一个关联和一些数据,但我也得到两个风帆生成的功能。我只是想从数据库和与该学校相关的所有评论中获得学校记录。如果我使用.populate(),我会收到错误:
错误:为.populate("Reviews")
构建联接说明时遇到意外错误
任何帮助都会很棒!
Schools.js模特
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
tableName:'schools',
attributes: {
SID: {
type: 'integer',
autoIncrement: true,
primaryKey: true,
size: 15
},
schoolName: {
type: 'text',
},
schoolCity: {
type: 'text',
},
schoolCountry: {
type: 'text',
},
schoolRegion: {
type: 'text',
},
schoolWebsite: {
type: 'text',
},
schoolCountryCode: {
type: 'text',
},
schoolNumbStudents: {
type: 'integer',
},
schoolNumbFaculty: {
type: 'integer',
size: 4,
defaultsTo: '0'
},
schoolCurriculum: {
type: 'text',
},
schoolLanguage: {
type: 'text',
},
schoolLangInstruction: {
type: 'text',
},
schoolonetoone: {
type: 'text',
},
schoolAccred: {
type: 'text',
},
schoolRatio: {
type: 'string',
size: 3
},
schoolMission: {
type: 'text',
},
schoolFiveWords: {
type: 'text',
},
schoolInitiatives: {
type: 'text',
},
schoolSalaryMin: {
type: 'text',
},
schoolSalaryMax: {
type: 'text',
},
schoolSalaryBA: {
type: 'text',
},
schoolSalaryBAPlus: {
type: 'text',
},
schoolSalaryMA: {
type: 'text',
},
schoolSalaryMAPlus: {
type: 'text',
},
schoolHealth: {
type: 'text',
},
schoolAirfare: {
type: 'text',
},
schoolAirfareProv: {
type: 'text',
},
schoolHousing: {
type: 'text',
},
schoolDependent: {
type: 'text',
},
schoolShipping: {
type: 'text',
},
schoolRetirement: {
type: 'text',
},
schoolreviews: {
collection: 'Reviews',
via: 'schoolID',
dominant: true
}
}
};
Reviews.js Model
module.exports = {
autoCreatedAt: false,
autoUpdatedAt: false,
tableName:'reviews',
attributes: {
RID: {
type: 'integer',
required: true,
autoIncrement: true,
primaryKey: true,
size: 15
},
schoolID: {
type: 'integer',
required: true,
size: 15,
model:'Schools'
},
profileID: {
type: 'integer',
required: true,
size: 15
},
active: {
type: 'integer',
required: true,
size: 1
},
dateCreated: {
type: 'date',
required: true,
defaultsTo: 'CURRENT_TIMESTAMP'
},
Q1: {
type: 'integer',
required: true,
size: 2
},
Q2: {
type: 'integer',
required: true,
size: 2
},
Q3: {
type: 'integer',
required: true,
size: 2
},
Q4: {
type: 'integer',
required: true,
size: 2
},
Q5: {
type: 'integer',
required: true,
size: 2
},
Q6: {
type: 'integer',
required: true,
size: 2
},
Q7: {
type: 'integer',
required: true,
size: 2
},
Q8: {
type: 'integer',
required: true,
size: 2
},
Q9: {
type: 'integer',
required: true,
size: 2
},
Q10: {
type: 'integer',
required: true,
size: 2
},
Q11: {
type: 'integer',
required: true,
size: 2
},
Q12: {
type: 'integer',
required: true,
size: 2
},
Q13: {
type: 'integer',
required: true,
size: 2
},
Q14: {
type: 'integer',
required: true,
size: 2
},
Q15: {
type: 'integer',
required: true,
size: 2
},
Q16: {
type: 'integer',
required: true,
size: 2
},
Q17: {
type: 'integer',
required: true,
size: 2
},
Q18: {
type: 'integer',
required: true,
size: 2
},
Q19: {
type: 'integer',
required: true,
size: 2
},
Q20: {
type: 'integer',
required: true,
size: 2
},
Q21: {
type: 'integer',
required: true,
size: 2
},
Q22: {
type: 'integer',
required: true,
size: 2
},
Q23: {
type: 'integer',
required: true,
size: 2
},
Q24: {
type: 'integer',
required: true,
size: 2
},
Q25: {
type: 'integer',
required: true,
size: 2
},
Q26: {
type: 'integer',
required: true,
size: 2
},
Q27: {
type: 'integer',
required: true,
size: 2
},
Q28: {
type: 'integer',
required: true,
size: 2
},
Q29: {
type: 'integer',
required: true,
size: 2
},
Q30: {
type: 'integer',
required: true,
size: 2
},
Q31: {
type: 'integer',
required: true,
size: 2
},
Q32: {
type: 'integer',
required: true,
size: 2
},
Q33: {
type: 'integer',
required: true,
size: 2
},
Q34: {
type: 'integer',
required: true,
size: 2
},
Q35: {
type: 'integer',
required: true,
size: 2
},
Q36: {
type: 'integer',
required: true,
size: 2
},
Q37: {
type: 'integer',
required: true,
size: 2
},
Q38: {
type: 'integer',
required: true,
size: 2
},
Q39: {
type: 'integer',
required: true,
size: 2
},
Q40: {
type: 'integer',
required: true,
size: 2
},
Q41: {
type: 'integer',
required: true,
size: 2
},
Q42: {
type: 'integer',
required: true,
size: 2
},
Q43: {
type: 'integer',
required: true,
size: 2
},
Q44: {
type: 'integer',
required: true,
size: 2
},
Q45: {
type: 'integer',
required: true,
size: 2
},
Q46: {
type: 'integer',
required: true,
size: 2
},
Q47: {
type: 'integer',
required: true,
size: 2
},
Q48: {
type: 'integer',
required: true,
size: 2
},
Q49: {
type: 'integer',
required: true,
size: 2
},
Q50: {
type: 'integer',
required: true,
size: 2
},
Q51: {
type: 'integer',
required: true,
size: 2
},
Q52: {
type: 'integer',
required: true,
size: 2
},
freeReview: {
type: 'text',
required: true
}
}
};
ReviewsController.js
module.exports = {
reviews: function(req, res){
Schools.findOne(1)
.populate('reviews')
.exec(function afterFind(err, theresults) {
if (err) return res.serverError(err);
return res.view({thereviews: theresults});
});
},
schoolreviews: function(req, res){
Reviews.find()
.exec(function afterFind(err, theresults) {
if (err) return res.serverError(err);
return res.view({thereviews: theresults});
});
},
testsearch: function(req, res){
Schools.findOne(1).populate('Reviews').exec(function(err, user) {
console.log(user);
});
},
};
答案 0 :(得分:0)
假设学校 - >评论是一对多的关系。您的模型设置不正确。如果您想查询学校和.populate每个学校的评论数据,那么您需要在Schools模型上添加评论作为一个集合,如下所示:
reviews: {
collection: 'reviews',
via: 'school'
}
注意:collection属性是小写的模型名称。
然后在评论模型中,将SchoolID属性替换为对Schools模型的引用。这将允许您在对评论进行查询时返回学校数据 - 例如:Reviews.find(...)。populate(' school')
school: {
required: true,
model: 'schools'
}
注意:当您为评论而不是 .populate()执行find()时,school属性将是学校记录的_id。