我正在使用MongoDB,Ember,Express和Node制作应用程序。
我的成绩模型定义如下:
export default DS.Model.extend({
mark: DS.attr(),
section: DS.attr(),
student: DS.belongsTo('student', { async: true }),
coursecode: DS.belongsTo('coursecode', { async: true }),
programrecord: DS.belongsTo('programrecord', { async: true })
});
我的学生模型定义如下:
export default DS.Model.extend({
number: DS.attr(),
firstName: DS.attr(),
lastName: DS.attr(),
DOB: DS.attr(),
resInfo: DS.belongsTo('residency',{ async: true }),
gender: DS.belongsTo('gender',{ async: true }),
country: DS.belongsTo('country',{ async: true }),
province: DS.belongsTo('province',{ async: true }),
city: DS.belongsTo('city',{ async: true }),
grades: DS.hasMany('grade', { async: true }),
});
我正在发出get请求,要求查询具有特定标记的所有成绩:
GET to /grades?mark=100
我也在提取请求以查询属于特定学生的成绩(不确定我是否正确地执行此操作,我正在传递学生对象的ID):
GET to /grades?student=56d33a7896c31b6b1e40446a
如何在server.js 文件中定义路由以响应此操作,这样我只能返回标记为100的成绩对象,或者只返回属于的等级对象一个学生?
答案 0 :(得分:0)
这是用于响应特定请求的路线(即获得标记为xxx的成绩对象或获得属于id yyy的学生的成绩对象)而不是返回所有成绩:
app.route('/grades')
.post(function (request, response) {
var grade = new GradeModel(request.body.grade);
grade.save(function (error) {
if (error) response.send(error);
response.json({grade: grade});
});
})
.get(function (request, response) {
var Grade = request.query.grade;
var mark = request.query.mark;
var student = request.query.student;
console.log(Grade);
console.log(mark);
console.log(student);
if (student) {
GradeModel.find({"student": student}, function (error, grades) {
if (error) response.send(error);
response.json({grade: grades});
});
}
else if (mark) {
GradeModel.find({"mark": mark}, function (error, grades) {
if (error) response.send(error);
response.json({grade: grades});
});
}
else {
GradeModel.find(function (error, grades) {
if (error) response.send(error);
response.json({grade: grades});
});
}
});
这是组件内部操作中的查询:
import Ember from 'ember';
export default Ember.Component.extend({
isGradesFormEditing: false,
store: Ember.inject.service(),
gradeModel: null,
grades: null,
ID: null,
actions: {
viewGrades () {
var self = this;
var myStore = this.get('store');
myStore.query('grade', {student: this.get('ID')}).then(function (grades) {
console.log(grades);
self.set('grades', grades);
});
}
}
});