从 collection.find() 中的数组传递对象的问题
problem with passing objects from array in collection.find()
我似乎无法弄清楚如何从 collection.find('{pid: patient[0][i].pid}') 中的数组传递对象。这是一个串联问题。
router.get('/fullsurveys/:doctor', async (request, res) => {
try{
const doctor = JSON.parse(request.params.doctor);
posts = [];
patients = [];
patients.push(await Patient.find(doctor));
for(var i = 0; i < patients[0].length; i++){
if(patients[0][i].patientstate == true){
posts.push(await FullSurvey.find('{pid: patient[0][i].pid}'));
}
}
res.json(posts);
}catch(err){
res.json({message: err});
}
});
您在 find 函数中传递了字符串化 json 并且 FullSuvery.find 中的变量名称似乎是错误的,您有 patient[0][i].pid
而它应该是 patients[0][i].pid
将您的代码更改为
router.get('/fullsurveys/:doctor', async (request, res) => {
try{
const doctor = JSON.parse(request.params.doctor);
posts = [];
patients = [];
patients.push(await Patient.find({doctor:doctor}));
for(var i = 0; i < patients[0].length; i++){
if(patients[0][i].patientstate == true){
posts.push(await FullSurvey.find({pid: patients[0][i].pid}));
}
}
res.json(posts);
}catch(err){
res.json({message: err});
}
})
我似乎无法弄清楚如何从 collection.find('{pid: patient[0][i].pid}') 中的数组传递对象。这是一个串联问题。
router.get('/fullsurveys/:doctor', async (request, res) => {
try{
const doctor = JSON.parse(request.params.doctor);
posts = [];
patients = [];
patients.push(await Patient.find(doctor));
for(var i = 0; i < patients[0].length; i++){
if(patients[0][i].patientstate == true){
posts.push(await FullSurvey.find('{pid: patient[0][i].pid}'));
}
}
res.json(posts);
}catch(err){
res.json({message: err});
}
});
您在 find 函数中传递了字符串化 json 并且 FullSuvery.find 中的变量名称似乎是错误的,您有 patient[0][i].pid
而它应该是 patients[0][i].pid
将您的代码更改为
router.get('/fullsurveys/:doctor', async (request, res) => {
try{
const doctor = JSON.parse(request.params.doctor);
posts = [];
patients = [];
patients.push(await Patient.find({doctor:doctor}));
for(var i = 0; i < patients[0].length; i++){
if(patients[0][i].patientstate == true){
posts.push(await FullSurvey.find({pid: patients[0][i].pid}));
}
}
res.json(posts);
}catch(err){
res.json({message: err});
}
})