Javascript 如何在react redux中的axios.get方法中传递登录用户的ID?
我是react-redux新手,当时我正在开发一个express应用程序,其中我选择react作为我的前端和redux商店概念。Javascript 如何在react redux中的axios.get方法中传递登录用户的ID?,javascript,reactjs,express,redux,Javascript,Reactjs,Express,Redux,我是react-redux新手,当时我正在开发一个express应用程序,其中我选择react作为我的前端和redux商店概念。我需要在我的axios.get方法中将登录用户作为参数传递 我该怎么做 我一直在做的是从数据库中获取用户id并使用它 但这种方法很乏味,对许多用户都不起作用 我可以在Redux开发工具中看到SET_CURRENT_USER正在显示登录的用户,但是是否有其他方法可以在执行ComponentDidMount()时在axios.get方法中传递此USER.id。 1.我的re
我需要在我的axios.get方法中将登录用户作为参数传递 我该怎么做 我一直在做的是从数据库中获取用户id并使用它 但这种方法很乏味,对许多用户都不起作用 我可以在Redux开发工具中看到SET_CURRENT_USER正在显示登录的用户,但是是否有其他方法可以在执行ComponentDidMount()时在axios.get方法中传递此USER.id。 1.我的react redux具有authActions
//Json Generator
export const registerJson = (userData, history) => dispatch => {
axios
.post("/api/users/registerJson/5d3ac84a86688123789e13b2/5d42d171e7ceef2a90245470/5d44256d8b3cf92af46e2c9b", userData)
.then(res => history.push("/JsonGenerator"))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
.
.
.
.
export const setCurrentUser = decoded => {
return {
type: SET_CURRENT_USER,
payload: decoded
};
};
{
auth:{
isAuthenticated:false=>true}
user:{
exp:1604591134,
iat:1573034208,
id:"5d36f73f67665a1740620f55",
name:"Pritam Kumar",
},
}
}
在URL中,您可以附加为查询参数您可以使用查询字符串
componentDidMount(){
const data = this.state.data //i put state data for example you can use any data source
const url=`http://localhost:5000/api/users/viewFarm/?userId=${data._id}`;
fetch (url,{
method: "GET"
}).then(response=> response.json()).then(result=>{
console.log(result);
this.setState({
farmList : result
});
});
}
在你的nodejs应用程序中
app.get('/api/users/viewFarm/', (req,res)=>{
//you can use router instead of app
console.log(req.query.userId)
})
{
auth:{
isAuthenticated:false=>true}
user:{
exp:1604591134,
iat:1573034208,
id:"5d36f73f67665a1740620f55",
name:"Pritam Kumar",
},
}
}
componentDidMount(){
const data = this.state.data //i put state data for example you can use any data source
const url=`http://localhost:5000/api/users/viewFarm/?userId=${data._id}`;
fetch (url,{
method: "GET"
}).then(response=> response.json()).then(result=>{
console.log(result);
this.setState({
farmList : result
});
});
}
app.get('/api/users/viewFarm/', (req,res)=>{
//you can use router instead of app
console.log(req.query.userId)
})