出现错误UnhandledPromiseRejectionWarning:错误:发送标头后无法设置标头。在Express JS API中

时间:2019-10-02 05:38:15

标签: node.js angular express node-modules

我正在使用jwtr开发express js api,当我运行api时会给我错误:

  

UnhandledPromiseRejectionWarning:错误:标头之后无法设置   已发送。

任何人都可以帮助我为什么我收到此错误,在这里我添加了我的代码,任何人都可以查看它,并帮助我解决此问题,

const requireAuthentication = async(req, res, next) => {
    try {
        const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxUWpuTDBhd2lTIiwiaWF0IjoxNTY5OTQwMjgyfQ.1xwX2OULW4RjKW4Ok13mwlJE8k95u-d0o7T6k5U9tjs'; //req.headers['vrc-access-token'];
        if (!token) return res.status(401).send('Failed to authenticate token.');
        let verify_token_data = await jwtr.verify(token, secret);
        if(typeof verify_token_data.jti != 'undefined') {
            req.body.username = verify_token_data.username;
            req.body.organization = verify_token_data.organization;
            req.body.userId = verify_token_data.id;
            req.body.organizationId = verify_token_data.organizationId;

            console.log("sdsd234");
            // create a new token
            const newToken = await jwtr.sign({
                username: verify_token_data.username,
                organization: verify_token_data.organization,
                id: verify_token_data.id,
                organizationId: verify_token_data.organizationId
            }, config['token-secret']);

            console.log(newToken);
            req.refreshToken = newToken;
            console.log('sdfdf');
            return await next();
        } else {
            return res.status(401).send('Failed to authenticate token.');
        }
    }  catch (error) {
        return res.status(401).send(error.message);
    }   
};

1 个答案:

答案 0 :(得分:0)

我以前曾遇到过这个问题,之所以会发生是因为您已经将响应发送回服务器,并且到达了再次发送响应的代码。

我认为问题出在何时 return await next()

您应该做的是: next()

const requireAuthentication = async(req, res, next) => {
    try {
        const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxUWpuTDBhd2lTIiwiaWF0IjoxNTY5OTQwMjgyfQ.1xwX2OULW4RjKW4Ok13mwlJE8k95u-d0o7T6k5U9tjs'; //req.headers['vrc-access-token'];
        if (!token) return res.status(401).send('Failed to authenticate token.');
        let verify_token_data = await jwtr.verify(token, secret);
        if(typeof verify_token_data.jti != 'undefined') {
            req.body.username = verify_token_data.username;
            req.body.organization = verify_token_data.organization;
            req.body.userId = verify_token_data.id;
            req.body.organizationId = verify_token_data.organizationId;

            // create a new token
            const newToken = await jwtr.sign({
                username: verify_token_data.username,
                organization: verify_token_data.organization,
                id: verify_token_data.id,
                organizationId: verify_token_data.organizationId
            }, config['token-secret']);

            console.log(newToken);
            req.refreshToken = newToken;
            next();
        } else {
            return res.status(401).send('Failed to authenticate token.');
        }
    }  catch (error) {
        return res.status(401).send(error.message);
    }   
};