javascript - Firebase 函数发送后无法设置标头
问题描述
我有一个 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 响应,其中包含我期望的数据。
这是我的函数/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});
}
});
});
推荐阅读
- c# - 如何在 Entity Framework Core 中禁用 MemoryCache?
- python - 弹丸不会变成图像错误如何修复?
- c++ - 在文件c ++中查找并删除整行
- python - 如何在 Appium Python 中找到已知子元素的父元素?
- nginx - nginx 入口拒绝带有 400 代码状态码的 HTTP POST 请求
- javascript - 从 JSON 对象递归构建查询字符串
- jquery - 使用 jQuery,如何在用户键入时突出显示所有新出现的和现有的单词?
- android-studio - 如何将搜索过滤器添加到每页的recyclerview的最后一页?
- c# - SystemResourceKey 引用的 WPF 视图样式
- python - 使用 BeautifulSoup 和 Pandas 在 Python 中进行动态网页抓取