[英]Firebase Cloud Function Error: "Cannot set headers after they are sent to the client"
I am new to firebase cloud functions and NodeJS, in general.一般来说,我是 firebase 云函数和 NodeJS 的新手。 I am trying to use Cloudinary's NodeJS SDK to rename an image as shown here:
我正在尝试使用 Cloudinary 的 NodeJS SDK 重命名图像,如下所示:
https://cloudinary.com/documentation/image_upload_api_reference#rename_method https://cloudinary.com/documentation/image_upload_api_reference#rename_method
I am using Firebase Cloud Function to handle the backend for this, but I am getting the following server log error when running the https function:我正在使用 Firebase Cloud Function 来处理后端,但是在运行 https function 时出现以下服务器日志错误:
> 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'
> }
My cloud function is functions/src/cloudinary/rename.js:
我的云function是
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
})
Anyone have any idea what I am doing wrong to cause this error?任何人都知道我做错了什么导致这个错误?
I prefer adding return
while returning sending back response so no code is executed after that (unless your use-case requires to) hence preventing any additional attempts to send response.我更喜欢在返回发送回响应时添加
return
,这样之后就不会执行任何代码(除非您的用例需要),从而防止任何额外的尝试发送响应。 In this case you have an additional res.send()
statement after your if-else
block.在这种情况下,您在
if-else
块之后有一个额外的res.send()
语句。 Try refactoring like this:尝试像这样重构:
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')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.