首页 > 解决方案 > 发布请求后出现“发送到客户端后无法设置标头”错误

问题描述

需要帮助解决一些奇怪的错误

这是托管在 heroku 上的 express 的 nodejs 服务

这是我的路由控制器代码

async function PopupConroller(req, res) {


        let credential = req.credential;
        let zoho_token = null;
        console.log('res :>> ', res);
        try {
            zoho_token  = await axios.post(`https://accounts.zoho.com/oauth/v2/token?client_id=${credential.client_id}&client_secret=${credential.client_secret}&refresh_token=${credential.refresh_token}&grant_type=refresh_token`);
            console.log('zoho_token.data :>> ', zoho_token.data);

        } catch (error) {
            console.log('ex 2 :>> ',error);
        }
        console.log('res :>> ', res);


       res.status(200).json({status:"ok"});

       return;

}

当服务收到请求时,代码抛出此错误 (来自 axios.post 请求没有错误)

回复:200 OK with no body

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
 at ServerResponse.setHeader (_http_outgoing.js:530:11)
 at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
 at ServerResponse.json (/app/node_modules/express/lib/response.js:264:10)
 at PopupConroller (/app/voicenter.js:261:28)
 at processTicksAndRejections (internal/process/task_queues.js:97:5)

删除这些行时一切正常响应:200 OK with {status:"ok"}

async function PopupConroller(req, res) {


        let credential = req.credential;
        let zoho_token = null;
        console.log('res1 :>> ', res);
        try {
            //zoho_token  = await axios.post(`https://accounts.zoho.com/oauth/v2/token?client_id=${credential.client_id}&client_secret=${credential.client_secret}&refresh_token=${credential.refresh_token}&grant_type=refresh_token`);
            //console.log('zoho_token.data :>> ', zoho_token.data);

        } catch (error) {
            console.log('ex 2 :>> ',error);
        }
        console.log('res2 :>> ', res);


       res.status(200).json({status:"ok"});

       return;

}

在第一个示例中,
当我检查 res 对象 (console.log res1) 时标头未发送,但在第二个示例 (console.log res2) 中,我注意到响应标头已发回。

这条线发送标题的方式???*需要说其他路线中的类似代码可以正常工作

zoho_token  = await axios.post(`https://accounts.zoho.com/oauth/v2/token?client_id=${credential.client_id}&client_secret=${credential.client_secret}&refresh_token=${credential.refresh_token}&grant_type=refresh_token`);

感谢您的帮助...谢谢

标签: javascriptnode.jsexpress

解决方案


我只是在这里猜测,这是你想要的吗?

要扩展下面的代码段,您会收到错误cannot set headers after they are sent to the client,因为axios.post()正在发出HTTP发布请求。

在您的代码中,您通过 axios 进行 HTTP 调用,以便发送标头。

之后,JS 解析器继续评估代码并在 try 块之后,因为没有错误,它评估 res.status() 以获取已发送的响应,因此出现错误。

因此,要解决这个问题,您可以像处理 post 请求一样处理,而无需再次将响应重新发送给客户端。然后,您可以使用“thenable”函数中的承诺响应“做某事”。

如果你想通过 axios 发布一个可选对象,你可以通过将参数传递给发布请求来做到这一点。

async function PopupConroller(req, res) {

        let credential = req.credential;
        let zoho_token = await axios.post("whatever", {"cat": "meaows"})
        .then((response)=> {
            // do something with the response
        })
        .catch((err) => console.log(err))
  
}

推荐阅读