我有两个系列: 生:
{
name: {type: String, required: true, unique: true},
address: {type: String, required: true},
phones: {
name: {type: String},
address: {type: String}
}
受试者:
{
name: {type: String, required: true, unique: true},
students: {studentid}
}
我想在API中创建一个函数,通过在URL中写入:http:apiurl/api/subjects/subject/studentid
它会返回属于该主题的学生的信息
到目前为止,我已经这样做了:
router.get('/subjects/:subject/:studentid', function (req, res) {
Subject.find({name: req.params.name}, function (err, subject) {
Student.find({id:req.params.studentid},function (err, student))
if (err) res.send(500, err.message);
console.log(student);
res.status(200).jsonp(student);
});
});
但我无法让它发挥作用
答案 0 :(得分:1)
答案 1 :(得分:0)
好像你没有“名字”参数,你的参数被命名为“subject”和“studentid”。
尝试更改路由器定义,如下所示:
router.get('/subjects/:subjectName/:studentid', function (req, res) {
Subject.find({name: req.params.subjectName}, function (err, subject) {
Student.find({id:req.params.studentid},function (err, student))
if (err) res.send(500, err.message);
console.log(student);
res.status(200).jsonp(student);
});
});