![](/img/trans.png)
[英]JavaScript Promise EsLint Problems when deploying Firebase-Cloud-Function
[英]Receving “500 Internal Server Error” on Post Request to Firebase-Cloud-Function Endpoint
我正在尝试使用axios
向我的axios
云功能发出POST
请求,以在react app中submit
表单。 但是,每次通过html页面响应发出请求时,都会出现“ 500
”错误。 此应用在启用javascriot的情况下效果最佳。
最新更新:
看来云功能代码没有问题。 而是更多的反应组件问题。 我使用Postman发送标头属性
Content-Type
设置为application/json
的POST
请求,并以原始格式{"email": "example_email"}
发送正文,并从云函数获得了预期的响应。 但是当从上面的react组件发送请求时,我得到一个html文件响应,说应用程序在启用javascript的情况下效果最佳
我曾尝试将Content-Type
设置为Application/json
和multipart/form-data
因为我怀疑这是一个问题,但还是没有运气。
以下是我的云功能代码,并对提交表单做出反应:
云功能
const functions = require('firebase-functions');
const cors = require('cors')({ origin: true })
const runThisFunc1 = require(./libs/runThisFunc1);
const runThisFunc2 = require(./libs/runThisFunc2);
exports.wizardFunc = functions.https.onRequest((request, response) => {
cors(request, response, () => {
let email = request.body.email;
try {
return runThisFunc1(email)
.then(data => {
console.log("Word Done by 1!");
return runThisFunc2(data);
})
.then(res => {
console.log("Word Done by 2!");
return response.status(200).send("Success");
})
.catch(err => {
console.error("Error: ", err.code);
return response.status(500).end();
});
}catch(err) {
return response.status(400).end();
}
});
});
React-Form-Component代码段
import axios from 'axios'
...
handleSubmit = e => {
e.preventDefault()
const { email } = this.state
axios({
method: 'post',
url: `${process.env.REACT_APP_CLOUD_FUNCTION_ENDPOINT}`,
data: { email: email },
config: {
headers: {
'Content-Type': 'multipart/form-data'
}
}
})
.then(res => {
//do something with reponse here
})
.catch(error => {
console.error(error)
})
}
...
我在代码中做错了什么,还是请求配置错误?
不知道为什么要这么做
const { email } = this.state
你能代替它并尝试
const email = this.state
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.