Firebase 云功能错误:“在将标头发送到客户端后无法设置标头”
Posted
技术标签:
【中文标题】Firebase 云功能错误:“在将标头发送到客户端后无法设置标头”【英文标题】:Firebase Cloud Function Error: "Cannot set headers after they are sent to the client" 【发布时间】:2021-11-21 10:43:37 【问题描述】:总的来说,我是 firebase 云功能和 NodeJS 的新手。我正在尝试使用 Cloudinary 的 NodeJS SDK 重命名图像,如下所示:
https://cloudinary.com/documentation/image_upload_api_reference#rename_method
我正在使用 Firebase Cloud Function 来处理后端,但在运行 https 函数时出现以下服务器日志错误:
> node:_http_outgoing:579
> throw new ERR_HTTP_HEADERS_SENT('set');
> ^
>
> Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
> at new NodeError (node:internal/errors:329:5)
> at ServerResponse.setHeader (node:_http_outgoing:579:11)
> at ServerResponse.header (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:771:10)
> at ServerResponse.json (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:264:10)
> at ServerResponse.send (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:158:21)
> at C:\Users\XXX\Desktop\work\XXX\functions\build\cloudinary\rename.js:37:27
> at C:\Users\nXXX\Desktop\work\Xxxx\functions\node_modules\cloudinary\lib\utils\index.js:1239:14
> at IncomingMessage.<anonymous> (C:\Users\XXX\Desktop\work\xxx\functions\node_modules\cloudinary\lib\uploader.js:505:9)
> at IncomingMessage.emit (node:events:381:22)
> at endReadableNT (node:internal/streams/readable:1307:12)
> code: 'ERR_HTTP_HEADERS_SENT'
>
我的云函数是functions/src/cloudinary/rename.js:
import cloudinary from 'cloudinary'
import * as functions from 'firebase-functions'
const cors = require('cors')( origin: true ) // Automatically allow cross-origin requests for browser fetch
cloudinary.config(
cloud_name: 'removed',
api_key: 'removed',
api_secret: 'removed'
)
export const rename = functions.https.onRequest((req, res) =>
return cors(req, res, () =>
try
cloudinary.v2.uploader.rename(
'aircraft_hzwme2',
'updated_name',
(error, result) =>
if (error)
res.status(500).send(error)
else
res.send(result)
)
return res.send('hi from rename function')
catch (error)
return res.status(500).send('There was an error in rename function')
) // end cors wrapper
)
有人知道我做错了什么导致这个错误吗?
【问题讨论】:
【参考方案1】:我更喜欢在返回发回响应时添加return
,因此此后不会执行任何代码(除非您的用例需要),从而防止任何额外的尝试发送响应。在这种情况下,您在 if-else
块之后还有一个额外的 res.send()
语句。尝试像这样重构:
try
cloudinary.v2.uploader.rename(
'aircraft_hzwme2',
'updated_name',
(error, result) =>
if (error)
return res.status(500).send(error) // <-- add return
else
return res.send(result) // <-- add return
)
// Remove this
// return res.send('hi from rename function')
catch (error)
return res.status(500).send('There was an error in rename function')
【讨论】:
以上是关于Firebase 云功能错误:“在将标头发送到客户端后无法设置标头”的主要内容,如果未能解决你的问题,请参考以下文章
错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法删除标头
错误 [ERR_HTTP_HEADERS_SENT] 在将标头发送到客户端后无法设置标头
错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头
我收到“错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头”
节点 JS:错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头
错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头。无法重定向与应有的页面不同的页面