node.js - 从 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});
}
});
解决方案
您在 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});
}
})
推荐阅读
- unit-testing - 当我使用自定义 axios 实例时,与 moxios 的玩笑不断超时
- caching - 如何使用 UUID 为会话管理配置 Redis 缓存
- python - 如何使用 Python 将文本文件转换为二维数组?
- android - 类型不匹配。必需:片段,找到:PlaceAutocompleteFragment
- git - 通过 git show 轻松访问合并的提交?
- c++ - 有什么方法可以对不同类型的变量进行相同的计算,只需一个函数?
- node.js - 用猫鼬和nodejs保存另一个文档的参考
- scala - 为什么在scala中展平和collect_list错误?无法解析符号
- javascript - 如何在javascript中触发mousedown vs click事件?
- react-native - 在本机反应中每 3 秒刷新一个函数