首页 > 解决方案 > 从 collection.find() 中的数组传递对象的问题

问题描述

我似乎无法弄清楚如何在 collection.find('{pid: patient[0][i].pid}') 中传递来自 arry 的对象。这是一个串联问题。

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});
    }
});

标签: node.jsmongodbexpressmean-stack

解决方案


您在 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});
    }
})

推荐阅读