在等待nodejs之后收到未定义的值,而不是返回的值"/>
。then方法在等待nodejs之后收到未定义的值,而不是返回的值
我对Promises还是陌生的,以为我现在或多或少已经了解了这个话题,但显然不是很好。我要求解释下面发生的事情,我想我已经了解了发生的事情,但是不是为什么 ...
我的误解在这段代码中:
(async () => {
try{
const result = await neoDB.getPatientById(requestedPatientID)
.then (result => {
console.log('2----------------'); //VIEW CONSOLE OUTPUT
console.log(result); //<---WHY RESULT IS UNDEFINED?? VIEW CONSOLE OUTPUT
resolve('Found Patient ' + requestedPatientID);
res.send(result); //sending patient's data as a response. If no patient was found, TBD is sent
});
}
catch (error) {
console.log('-- in catch');
console.log(error);
}
})();
和neoDB.getPatientById(id):
async function getPatientById(id){
const session = driver.session();
try {
const result = await session.run('MATCH (p:Patient {id : ' + id + '}) RETURN p')
.then(result => {
const PatientJson = convertDBPatientObjToJson(result.records[0]._fields[0].properties)
console.log(PatientJson); //VIEW CONSOLE OUTPUT
console.log('1----------------'); //VIEW CONSOLE OUTPUT
return PatientJson;
});
}
catch (error){
console.log('-- in catch');
console.log(error);
}
finally {
await session.close()
}
}
我知道,如果有返回,则按约定的方式返回一个Promise,但是.then方法(在第一段代码中)为什么不等待返回的值?
控制台输出:
node localDB.js
localDB-MS started on port: 3002
{id: 1, phone_number: 11111, a: patient1, b: 25, c: true}
1----------------
2----------------
undefined
您可以看到输出时间轴与逻辑相对应,但是结果的内容出了问题。
感谢您的帮助,我试图了解这里发生的事情,所以请加上一两个字:)
回答如下:更多推荐
。then方法在等待nodejs之后收到未定义的值,而不是返回的值
发布评论