我正在做一个回送项目,我需要从模型中获取文档。我从Model获得结果,但是我的要求是我们需要获取前5个文档结果。在第一次迭代中,searchId是avialabe,因此我可以使用此searchId进行下一次搜索,并根据限制使跳过值递增。
但是问题是迭代仅执行一次。有人可以看看吗?任何帮助都是可观的。
function fetchProcedureRequest(availableJob) {
let jobPatientId = availableJob.data.pId;
let userId = availableJob.data.uid;
let ProcedureRequestModel = app.models.ProcedureRequest;
var filter = {
where: { subject: `Patient/${jobPatientId}`, status: 'completed'},
limit : 5,
skip : 0
}
var options = {};
var searchResultId = '';
var fetchedCount = 0;
var searchTotalCount = '';
var i=0;
do {
// i++;
async function dataTraverse(jobPatientId, ProcedureRequestModel, filter) {
try {
return await ProcedureRequestModel.find(filter, options, (err, results) => {
if(err) {
console.log('Error: ', err);
} else {
searchResultId = options.searchId;
searchTotalCount = options.totalCount;
if(searchResultId) {
console.log('Search Result Id Available');
filter.where = { subject: `Patient/${jobPatientId}`, status: 'completed'};
filter.skip += filter.limit;
fetchedCount = filter.skip;
console.log(filter.skip);
console.log('Fetched Count', fetchedCount);
console.log('Total Count', searchTotalCount);
// console.log(results);
}
}
})
} catch(e) {
console.log(e);
}
}
dataTraverse(jobPatientId, ProcedureRequestModel,filter);
} while(fetchedCount < searchTotalCount);
}
fetchProcedureRequest(availableJob);
输出获取:
Search Result Id Available
5
Fetched Count 5
Total Count 22
我需要的输出:
Fetched Count 5
Fetched Count 10
Fetched Count 15
Fetched Count 20