【发布时间】:2023-04-03 21:54:01
【问题描述】:
我有一个 Firebase 函数,我无法从我的 React.js 网络应用程序中查询,但通过 Postman 没有问题。
当我尝试通过网络应用发出请求时,我的 Firebase 控制台中出现以下错误:
错误:发送后无法设置标头。
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/worker/node_modules/express/lib/response.js:767:10)
at ServerResponse.json (/worker/node_modules/express/lib/response.js:264:10)
at ServerResponse.send (/worker/node_modules/express/lib/response.js:158:21)
at exports.uploadImage.functions.https.onRequest (/srv/index.js:21:13)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
在网络应用程序上,我收到 500 错误响应,但没有返回任何数据。
我的请求是这样提出的:
const data = {
image: 'image url here',
options: {
tags: 'document,doctor',
}
};
axios.post('*** FUNCTION URL HERE***/uploadImage', data, {
headers: {
'Content-Type': 'application/json',
},
})
但是,如果我通过 Postman 提出请求,我会收到 200 响应,其中包含我期望的数据。
这是我的 functions/index.js 文件的样子:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cloudinary = require('cloudinary');
const cors = require('cors')({origin: true});
admin.initializeApp();
exports.uploadImage = functions.https.onRequest(async (req, res) => {
cors(req, res, () => {});
cloudinary.config({
cloud_name: 'config here',
api_key: 'config here',
api_secret: 'config here',
});
try {
const data = Object.apply({folder: 'myortho'}, req.body.options);
const response = await cloudinary.uploader.upload(req.body.image, data);
return res.send(response);
}catch(error) {
return res.send(500, {message: 'Error uploading image', error});
}
});
我看到了许多与我遇到的错误有关的问题,但其中许多问题都与人们的路由问题有关(这不是我关心的问题,因为这是一个云函数,而不是我自己的 Node.js 后端服务器)。
这也是一个我无法理解的错误,因为它是通过 Postman 成功的,但不是通过我的浏览器,所以我真的很困惑。
【问题讨论】:
标签: javascript node.js reactjs firebase google-cloud-functions