在循环中使用Find()通过PouchDB调用函数中的值



我正试图循环Find((Mango Query的结果,并生成一个all-to-other函数,以获取要在报告中使用的额外数据。

我正在循环Find((查询中的患者文档列表,但我想通过调用执行查询的函数从另一个"就诊"文档列表中提取"上次就诊",但我遇到了问题。

我可以调用函数"Get_Static_Value((",它会返回一个值,但当我将Patient_ID发送到函数"Get_Last_Visit(Patient_ID("时,尽管调用了该函数,但该值会返回为"未定义",并将"Vist_Date"写入控制台。

我相信我的问题是因为查询中的promise没有得到解决,但我不确定函数处理后将值返回到循环中的语法。

我看了文件https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html在"新手错误#2:WTF,我如何将forEach((与promise一起使用?"一节中,我认为它指出了我的语法问题:

db.allDocs({include_docs: true}).then(function (result) {
return Promise.all(result.rows.map(function (row) {
return db.remove(row.doc);
}));
}).then(function (arrayOfResults) {
// All docs have really been removed() now!
});

然而,上面的代码适用于alldocs,而不是find((,所以我有点拘泥于如何对find((查询的结果处理相同的方法。

我创建了一个JSfiddle来展示我的代码并演示我的问题。

https://jsfiddle.net/movitico/gkb89uyf/

// Create the Database
var db = new PouchDB('patient_test');
// Add Patient Documents
function Add_Patients() {
db.bulkDocs([{
_id: '1',
type: 'patient',
Patient_Name: 'Patient 1',
Patient_Status: 'Active'
},
{
_id: '2',
type: 'patient',
Patient_Name: 'Patient 2',
Patient_Status: 'Active'
},
{
_id: '3',
type: 'patient',
Patient_Name: 'Patient 3',
Patient_Status: 'Active'
}
]);
}
function Add_Visits() {
// Add Visit Documents
db.bulkDocs([{
_id: 'v1',
type: 'visit',
Patient_ID: '1',
Visit_Date: "06/01/2018"
},
{
_id: 'v2',
type: 'visit',
Patient_ID: '1',
Visit_Date: "05/01/2018"
},
{
_id: 'v3',
type: 'visit',
Patient_ID: '1',
Visit_Date: "02/22/2018"
},
{
_id: 'v4',
type: 'visit',
Patient_ID: '2',
Visit_Date: "02/22/2014"
},
{
_id: 'v5',
type: 'visit',
Patient_ID: '2',
Visit_Date: "02/22/2000"
},
{
_id: 'v6',
type: 'visit',
Patient_ID: '2',
Visit_Date: "02/22/1987"
}
]);
}
function Load_Patients() {
$('#patient_list').empty();
db.createIndex({
index: {
fields: ['Patient_Name', 'type', 'Patient_Status']
}
}).then(function(result) {
db.find({
selector: {
Patient_Name: {
$gt: true
},
type: {
$eq: 'patient'
},
Patient_Status: {
$eq: 'Active'
}
},
sort: [{
"Patient_Name": "asc"
}]
}, function(error, response) {
console.log(response);
for (i in response.docs) {
var Static_Value = Get_Static_Value();
var Last_Visit = Get_Last_Visit(response.docs[i]._id);
$('#patient_list').append('<li>' + response.docs[i]._id + ' ' + response.docs[i].Patient_Name + ' [' + Static_Value + ']' + ' ' + Last_Visit + '</li>');
}
})
});
}
Add_Patients();
Add_Visits();
$('#button_load_patients').unbind().click(function(e) {
Load_Patients();
});
function Get_Static_Value() {
return 'I am static';
}
function Get_Last_Visit(Patient_ID) {
db.createIndex({
index: {
fields: ["Visit_Date", "type"]
}
}).then(function(result) {
db.find({
selector: {
Visit_Date: {
$gt: true
},
type: {
$eq: 'visit'
},
Patient_ID: {
$eq: Patient_ID
}
},
sort: [{
"Visit_Date": "desc"
}],
limit: 1
}).then(function(response) {
console.log(response);
if (response.docs.length > 0) {
Visit_Date = response.docs[0].Visit_Date;
} else {
Visit_Date = 'Never';
}
console.log(Visit_Date);
return Visit_Date;
});
})
}

一旦我返回了"Visit_Date"值,我就会使用MomentJS对其进行操作,并将其从附加到div的结果中包括或排除。

如果能就我做错了什么提出建议,我将不胜感激。

在一位同事的帮助下,我找到了这个问题的解决方案,并将发布解决方案,因为我确信有人和我一样困惑。

在我的代码中需要更改的是,对"Get_Last_Visit"函数的调用需要一个.then来返回promise的值。我还需要添加"let I",使"I"变量成为全局变量,并在函数中可用:

for (let i in response.docs) {
var Static_Value = Get_Static_Value();
Get_Last_Visit(response.docs[i]._id)
.then(function(Last_Visit) {
$('#patient_list').append('<li>' + response.docs[i]._id + ' ' + response.docs[i].Patient_Name + ' [' + Static_Value + ']' + ' ' + Last_Visit + '</li>');
})
}

这是一个更新的jsfiddle。

https://jsfiddle.net/movitico/9xorLham/

最新更新