Reactjs 错误:";请求失败,状态代码为“500”;在发布到API firebase时

Reactjs 错误:";请求失败,状态代码为“500”;在发布到API firebase时,reactjs,firebase,google-cloud-functions,Reactjs,Firebase,Google Cloud Functions,发布到API时出现错误:“请求失败,状态代码为500” 在此之前,我在添加应用程序时遇到了跨源错误。使用(cors())获取内部错误500 将以下内容添加到我的代码中 const express=require('express') const app=express(); app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); next(

发布到API时出现错误:“请求失败,状态代码为500”
在此之前,我在添加应用程序时遇到了跨源错误。使用(cors())获取内部错误500
将以下内容添加到我的代码中

  const express=require('express')
    const app=express();
    app.use((req, res, next) => {
        res.header('Access-Control-Allow-Origin', '*');
        next();
      });
    app.use(cors());

   app.post("/project",(req,res)=>cors(req,res,()=>{
    const newProject={
        title:req.body.title,
        description:req.body.description,
        status:req.body.status,
        startdate:req.body.startdate,
        enddate:req.body.enddate,
        supervisor:req.body.supervisor,
        createdAt:admin.firestore.Timestamp.fromDate(new Date())
    }
    admin.firestore().collection("Projects")
    .add(newProject)
    .then(doc=>{
        return res.json({message:`Project ${doc.id} created successfully`})
    })
    .catch(err=>{
        res.status(500).json({error:`Something went wrong, ${err}`})
    })
}))
以下是客户端代码:

const newProject=JSON.stringify({
    title:project.name,
    description:"",
    status:project.status,
    startdate:project.startdate,
    enddate:project.enddate,
    supervisor:project.supervisor,
})
axios.post("https://us-central1-flair-d7b59.cloudfunctions.net/api/project",newProject)
.then(res=>{
  this.setState({ open: false });
  Swal.fire({
    icon:"success",
    toast:true,
    title: res,
    position: 'top-end',
    showConfirmButton: false,

    showClass: {
      popup: ''
    },
    timer: 2500
  })
})
.catch(err=>{
  console.log(err)
  this.setState({loading:false})
  Swal.fire({
    icon:"error",
    toast:true, 
    title:"Something went wrong, Please try again",
    position: 'top-end',
    showConfirmButton: false,

    showClass: {
      popup: ''
    },
    timer: 2500
  })
})

尝试了很多,但没有解决办法。谢谢

我建议使用中间件来处理CORS握手,而不是手动进行

app.use((请求、恢复、下一步)=>{
res.header('Access-Control-Allow-Origin','*');
next();
});

app.use(cors());
在尝试使用请求体之前,也不会将其解析为JSON。这可以通过使用中间件来实现。因为您是用JSON响应的,所以我还假设您是用JSON发送数据的

const functions=require('firebase-functions');
const admin=require('firebase-admin');
const express=require('express');
const cors=需要(“cors”);
const jsonBodyParser=require('body-parser').json();
admin.initializeApp();
常量app=express();
app.use(cors());//启用所有源
app.post(“/project)”,(请求、回复)=>{
jsonBodyParser(req,res,(err)=>{//将主体解析为JSON
如果(err){//手动链接到“next()”时,不要忘记处理错误
json({错误:'Failed to parse body as json'});
返回;
}
const newProject={
标题:req.body.title,
描述:req.body.description,
状态:req.body.status,
起始日期:req.body.startdate,
enddate:req.body.enddate,
主管:请求主体主管,
createdAt:admin.firestore.Timestamp.fromDate(新日期())
}
admin.firestore()集合(“项目”)
.add(新建项目)
。然后(doc=>{
返回res.json({message:`Project${doc.id}已成功创建`})
})
.catch(错误=>{
json({error:`Something出错,${err}`})
});
});
});
exports.app=functions.https.onRequest(app);

post more details\uuo怎么知道发生了什么事?Hello@RenaldoBalaj提供了代码,请查看。是的,我使用过,但没有结果。您有很多重复的代码。我将重构它,只需几分钟。获取跨源异常(原因:CORS请求未成功)。重新部署函数并重新测试。此外,在客户端上,在将数据传递给axios之前,不要对数据进行字符串化,只需将对象以原始格式传递即可。