。then方法在等待nodejs之后收到未定义的值,而不是返回的值

编程入门 行业动态 更新时间:2024-10-19 12:39:00

。then方法<a href=https://www.elefans.com/category/jswz/34/1754058.html style=在等待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之后收到未定义的值,而不是返回的值

本文发布于:2024-05-07 12:05:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在等待   而不是   未定义   方法   nodejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!