Node.js:不等待承诺的函数

qyzbxkaa  于 2022-12-22  发布在  Node.js
关注(0)|答案(2)|浏览(144)

我遇到了一个不等待承诺的函数问题。
我有一个包含post方法的路由,它将body推送到控制器,然后控制器将其发送到中间件,以实际处理保存到MongoDB的操作,中间件将承诺的对象返回给控制器。
控制器正在完成,无需等待中间件的完成。
中间件是异步的,控制器有一个wait,然后是a。然后甚至更进一步。
主计长

export async function createPlayer(req,res) {
    //creates a Player in the database
    try {
        let results = '';
        results = await PlayerFunction.createPlayer(req.body)
        .then(console.log(results))
        .then(res.status(200).send(results))
  
    } catch (error) {
        log_error('Errored trying to create Player in controller.', error);
        res.status(404).send('Error Has Occured. An Admin has been contacted.');
    }

};

中间件

export async function createPlayer(req_body) {
    //creates a Player in the database
    try {
        //set new player object
        let new_Player = new Player(req_body);
        new_Player.save((error) => {
            if (error) {
                log_error('Trying to create a Player.', error)
                return { success: 'error', body: 'Error Has Occured. An Admin has been contacted.' };
            } else {
                console.log(new_Player);
                return { success: true, body: new_Player };
            };
        });
    } catch (error) {
        log_error('Errored trying to create a player.', error);
        return { success: 'error', body: 'Error Has Occured. An Admin has been contacted.' };
    }
};

我尝试使用.then承诺捕获从中间件返回的结果,然后将结果返回给用户。

fquxozlt

fquxozlt1#

我发现. save((error)=〉函数并不跟随调用它自己所做的等待,如果你删除Save上的错误处理,并依赖于整个try catch中的catch,它将跟随等待。
控制器:

export async function createPlayer(req,res) {
//creates a Player in the database
try {
    
    res.status(200).send(await PlayerFunction.createPlayer(req.body));

} catch (error) {
    log_error('Errored trying to create Player in controller.', error);
    res.status(404).send('Error Has Occured. An Admin has been contacted.');
}

};
中装

export async function createPlayer(req_body) {
//creates a Player in the database
try {
    let new_Player = new Player(req_body);
    await new_Player.save();
    return { success: true, body: new_Player };

} catch (error) {
    log_error('Errored trying to create a player.', error);
    return { success: 'error', body: 'Error Has Occured. An Admin has been contacted.' };
}

};

a8jjtwal

a8jjtwal2#

async中,函数需要返回基于承诺的值。
一个Promise,它将使用异步函数返回的值进行解析,或因异步函数引发异常或在异步函数内未捕获异常而被拒绝。
因此,在中间件中,createPlayer()需要返回基于承诺的值。

export async function createPlayer(req_body) {
    //creates a Player in the database
    try {
        //set new player object
        let new_Player = new Player(req_body);
        new_Player.save((error) => {
            if (error) {
                log_error('Trying to create a Player.', error)
                return Promise.reject(JSON.stringify({ success: 'error', body: 'Error Has Occured. An Admin has been contacted.' }));
            } else {
                console.log(new_Player);
                return Promise.resolve(JSON.stringify({ success: true, body: new_Player }));
            };
        });
    } catch (error) {
        log_error('Errored trying to create a player.', error);
        return Promise.reject(JSON.stringify({ success: 'error', body: 'Error Has Occured. An Admin has been contacted.' }));
    }
};

相关问题