Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将数据从react组件传递到代理(节点服务器)_Node.js_Reactjs_Proxy_Axios - Fatal编程技术网

Node.js 将数据从react组件传递到代理(节点服务器)

Node.js 将数据从react组件传递到代理(节点服务器),node.js,reactjs,proxy,axios,Node.js,Reactjs,Proxy,Axios,我在这个react应用程序中设置了一个代理来绕过预期api的CORS。我无法将数据从react组件传递到代理(nodeJS服务器)。我已经阅读了一些链接,如和,但仍然没有线索 /*反应组件*/ 从“React”导入React,{useState}; 从“axios”导入axios; 导出默认函数邮件(){ const[emailInput,setEmailInput]=useState(“”) const getMail=()=>{ get(“/list/members”)。然后(json=>{

我在这个react应用程序中设置了一个代理来绕过预期api的CORS。我无法将数据从react组件传递到代理(nodeJS服务器)。我已经阅读了一些链接,如和,但仍然没有线索

/*反应组件*/
从“React”导入React,{useState};
从“axios”导入axios;
导出默认函数邮件(){
const[emailInput,setEmailInput]=useState(“”)
const getMail=()=>{
get(“/list/members”)。然后(json=>{
log(json.data);
});
};
const subscribe=(电子邮件)=>{
console.log('内部订阅')
axios.post(“/member”,电子邮件)
.then(data=>console.log('post successed'))
.catch(err=>console.log(err))
}
常量handleSubmit=e=>{
e、 预防默认值();
常量电子邮件={
电子邮件地址:`${emailInput}`,
状态:“已订阅”
};
订阅(电子邮件)
};
常数handleChange=(e)=>{
setEmailInput(例如target.value)
}
返回(
{" "}
);
}
在节点服务器中,我有

app.post(“/member)”,(请求、回复)=>{
常量电子邮件={
电子邮件地址:axios1234@gmail.com",
状态:“已订阅”
};
轴心柱(
“https://”,
电子邮件,
{
事实上,
认证:{
用户名:“anystring”,
密码:“
}
}
)。然后(json=>{
res.send(json.data)
}).catch(错误=>{
控制台日志(err);
})
});

我已经检查了react前端应用程序和代理服务器之间的管道是否正常工作。我还检查了
app.post(“/member”)中的
req
res
(req,res)
但是发现
req.body
未定义的
,并且找不到从react函数组件传入的
电子邮件
对象。我在这里遗漏了什么吗?

是否使用body解析器?如果没有,请安装并将代码更改为:

const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post("/member", (req, res) => {
  const email = req.body.email_address;

  axios.post(
    "https://<apilink>",
    email,
    {
      withCredentials: true,
      auth: {
        username: "anystring",
        password: "<apikey>"
      }
    }
  ).then(json => {
    res.send(json.data)
  }).catch(err => {
    console.log(err);
  })
});
constbodyparser=require('body-parser');
use(bodyParser.json());
应用程序发布(“/成员)”,(请求、回复)=>{
const email=req.body.email\u地址;
轴心柱(
“https://”,
电子邮件,
{
事实上,
认证:{
用户名:“anystring”,
密码:“
}
}
)。然后(json=>{
res.send(json.data)
}).catch(错误=>{
控制台日志(err);
})
});