[英]How can I pass the ID of logged-in user in the axios.get method in react-redux?
I am new to react-redux,when I was developing an express app in which I choose react as my frontend and redux store concept.我是 react-redux 的新手,当时我正在开发一个快速应用程序,在其中我选择 react 作为我的前端和 redux 商店概念。
I need to pass the logged in user as a param in my axios.get method How can i do this?我需要在我的 axios.get 方法中将登录用户作为参数传递我该怎么做?
What I have been doing is getting the user._id from database and using it but this approach is tedious and will not work for a number of users我一直在做的是从数据库中获取 user._id 并使用它,但是这种方法很乏味,并且不适用于许多用户
componentDidMount(){
const url="http://localhost:5000/api/users/viewFarm/5d3ac84a86688123789e13b2";
fetch (url,{
method: "GET"
}).then(response=> response.json()).then(result=>{
console.log(result);
this.setState({
farmList : result
});
});
}
I can see in Redux Dev Tool the SET_CURRENT_USER is displaying the logged in user but is there any other way so that I can pass this user.id in the axios.get method while doing ComponentDidMount().我可以在 Redux 开发工具中看到 SET_CURRENT_USER 正在显示登录用户,但是还有其他方法可以在执行 ComponentDidMount() 时在 axios.get 方法中传递这个 user.id。 1. My react-redux have authActions 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
};
};
import { SET_CURRENT_USER, USER_LOADING } from "../actions/types";
const isEmpty = require("is-empty");
const initialState = {
isAuthenticated: false,
user: {},
loading: false
};
export default function(state = initialState, action) {
switch (action.type) {
case SET_CURRENT_USER:
return {
...state,
isAuthenticated: !isEmpty(action.payload),
user: action.payload
};
case USER_LOADING:
return {
...state,
loading: true
};
default:
return state;
}
}
import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers";
const initialState = {};
const middleware = [thunk];
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
export default store;
Redux dev tool o/p Redux 开发工具 o/p
{
auth:{
isAuthenticated:false=>true}
user:{
exp:1604591134,
iat:1573034208,
id:"5d36f73f67665a1740620f55",
name:"Pritam Kumar",
},
}
}
In the URL, you can append as a query parameter在URL中,可以将append作为查询参数
You can use query strings.您可以使用查询字符串。
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
});
});
}
and in your nodejs app在你的 nodejs 应用程序中
app.get('/api/users/viewFarm/', (req,res)=>{
//you can use router instead of app
console.log(req.query.userId)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.