我一直在关注StackOverflow上的大量教程和问题,但仍然卡住了。
在Sails.js中我试图将两个模型连接在一起,这些模型已经在MySQL数据库中有数据,但是我无法让Sails通过关联输出数据。我可以让Sails独立输出每个模型,但是当我尝试连接它们时,我只会得到错误。我正在努力使关联和输出正确链接。
我正在尝试输出所有评论并加入学校信息以输出到视图中的表格。
我的模特是: API /模型/ Schools.js
module.exports = {
connection: 'mysql',
tableName: 'schools',
autoCreatedAt: false,
autoUpdatedAt: false,
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',
},
reviews: {
collection: 'review',
via: 'theschool'
}
}
};
API /模型/ Reviews.js
module.exports = {
connection: 'mysql',
tableName: 'reviews',
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
RID: {
type: 'integer',
required: true,
autoIncrement: true,
primaryKey: true,
size: 15
},
schoolID: {
type: 'integer',
required: true,
size: 15
},
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
},
theschool: {
model: 'schools',
via: 'reviews'
}
}
};
我的控制器代码是。
module.exports = {
reviews: function(req, res){
Schools.find().populate('reviews').exec(function afterFind(err, review) {
if (err) return res.serverError(err);
console.log(review);
return res.view({thereviews: review});
});
},
};
感谢您的帮助!
答案 0 :(得分:0)
我为自己想出来了。对于任何被困的人......
在Schools.js中我修改了模型属性。
reviews: {
collection: 'review',
via: 'theschool'
}
到
reviews: {
collection: 'reviews',
via: 'schoolID',
dominant: true
}
via引用了Reviews.js模型中的schoolID
在Reviews.js中我修改了模型。我删除了属性
theschool: {
model: 'schools',
via: 'reviews'
}
并将schoolID属性更新为
schoolID: {
type: 'integer',
required: true,
size: 15,
model:'schools'
},
然后是我的控制器。
reviews: function(req, res){
Schools.findOne(1)
.populateAll()
.exec(function afterFind(err, theresults) {
if (err) return res.serverError(err);
return res.view(theresults);
});
},
它没有完全按照我的意愿输出数据,但它至少将两个表/模型连接在一起。我还认为技巧是需要成为数据库中的表字段。