Firebase Cloud Function Error: "Cannot set headers after they are sent to the client"

Firebase Cloud Function Error: "Cannot set headers after they are sent to the client"

总的来说,我是 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
})

有人知道我做错了什么导致了这个错误吗?

我更喜欢在返回发送回响应时添加 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')
}