简体   繁体   English

Firebase 云 Function 错误:“将标头发送到客户端后无法设置标头”

[英]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.

相关问题 node.js:错误 [ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头 - node.js : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 错误:运行时出现“找不到模块” firebase 云 function - Error: "Cannot find module" while running firebase cloud function Firebase 云 function 部署错误 - Firebase cloud function deploy error 谷歌云/firebase 存储错误:没有 `client_email` 无法签署数据。 ...名称:'SigningError' - Google cloud/firebase storage Error: Cannot sign data without `client_email`. ... name: 'SigningError' 解析错误:遵循 Firebase Cloud Functions 初始化说明后无法读取文件 '\tsconfig.json' eslint - Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions 无法在 flutter 中部署 Firebase 云 Function - Cannot deploy Firebase Cloud Function in flutter 云 Function:尝试从 Firebase RTDB(打字稿)读取时,错误客户端处于脱机状态 - Cloud Function: ERROR Client is offline when trying to read from Firebase RTDB (Typescript) Firebase 云函数返回内部错误 - Firebase Cloud Function returns INTERNAL error Firebase 可调用云 Function CORS 错误使用 Z035489FF8D092741943E4A83241F5 - Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite Firebase 云功能:部署 function 时出错 - Firebase Cloud Functions: Error while deploying function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM