nodejs 承诺给出未处理的承诺拒绝,这似乎得到了正确的处理

问题是我收到 UNhandledPromiseRejection 错误,尽管我认为我已经处理了所有情况。代码从 profileRoutes 流向 Controller,再到错误首先出现的 Utils。


在 profileRoutes.js 里面


router.get('/:username', async (r, s) => {

    try{

        let profileData = await getProfileData(r.params.username);

        s.json({ success: true, payload: profileData });

    }catch(err){

        console.log('ending request processing by responding a error');

        s.status(500).json({ success: false, message: 'err[0].message' });

    }

});


在 controllers/index.js 里面



const fetchQueue = [getUserRepos];

async function getProfileData(username) {

    let profileData = {};

    try{


        let results = await Promise.all(fetchQueue.map(item => item(username)));

        for (let i = 0; i < results.length; i++) {

            profileData[getKeys[i]] = results[i];

        }

        return profileData;


    }catch(err){

        console.log('error log in controller/index getProfileData function');

        throw err;

    }

}


const getUserRepos = async (username) => {

    

    try {

        // const res = await utils.gqlSender(username, 'userRepos', { createdAt });

        const res = await utils.gqlSender(username, 'userReposData');

        return res.user.repositories;

    } catch (err) {

        console.log('error log in controller/index getUserRepos function');

        throw err;

    }

};



在 utils/index.js 里面



const gqlSender = async (username, type, opt = {}) => {

    

    axios.post('', {

        query: gqlQuery(username, type, opt) // generates a needed graphQL query

    }).then(res => {

        if(res.data.errors) {  // this is where the error is recieved and so i reject promise.                     

            console.log('bef@@@re'); 

            return Promise.reject (res.data.errors);

        }

        console.log('###',res.data);

        return res.data;

    }).catch(err => {

        console.log('error in making axios request inside utils/index gqlSender function');

        throw err;

        // return Promise.reject(err);

    });

我不认为我错过了任何承诺拒绝。任何帮助表示赞赏。谢谢。


www说
浏览 138回答 1
1回答

GCT1015

您的gqlSender功能不会return拒绝将被拒绝的承诺,因此不会在任何地方处理。你应该写const gqlSender = (username, type, opt = {}) => {&nbsp; &nbsp; return axios.post('', {//&nbsp; ^^^^^^&nbsp; &nbsp; &nbsp; &nbsp; query: gqlQuery(username, type, opt) // generates a needed graphQL query&nbsp; &nbsp; }).then(res => {&nbsp; &nbsp; &nbsp; &nbsp; if (res.data.errors) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('error in making axios request inside utils/index gqlSender function');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw res.data.errors;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('###',res.data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return res.data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; });};或者const gqlSender = async (username, type, opt = {}) => {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^&nbsp; &nbsp; const res = await axios.post('', {&nbsp; &nbsp; &nbsp; &nbsp; query: gqlQuery(username, type, opt) // generates a needed graphQL query&nbsp; &nbsp; });&nbsp; &nbsp; if (res.data.errors) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('error in making axios request inside utils/index gqlSender function');&nbsp; &nbsp; &nbsp; &nbsp; throw res.data.errors;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log('###',res.data);&nbsp; &nbsp; &nbsp; &nbsp; return res.data;&nbsp; &nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript