我有一个 Firebase 函数,无法从 React.js Web 应用程序查询,但通过 Postman 没有问题。
当我尝试通过网络应用发出请求时,我的 Firebase 控制台中出现以下错误:
Error: Can't set headers after they are sent.
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 成功,但通过我的浏览器失败,所以我真的很困惑。
最佳答案
当您使用 cors 时,您应该将代码放入传递给它的回调函数中。您根本没有使用该回调,而发生的情况是您的代码正在尝试发送两个响应。它应该更像是这样的:
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});
}
});
});
关于javascript - Firebase 函数发送后无法设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54465552/