看看下面的代码。我将userId
传递到我的动作中,但它的值从未被使用过。也许我使用find
函数的方式是错误的,一些帮助是感激的。
export const getUserInfo = (userId) => dispatch =>{
axios.get("http://localhost:5000/users")
.then((res) =>{
const userInfo = res.find((userId) =>{
return res.data.id === userId;
})
dispatch({
type: USER_INFO,
payload: userInfo
})
})
}
编辑:我想做的是将res.data.id
与给定的userId
匹配。
删除查找函数参数中的userId
,并将userId
替换为data
export const getUserInfo = (userId) => dispatch =>{
axios.get("http://localhost:5000/users")
.then((res) =>{
const userInfo = res.find((data) =>{ //<= change the param
return data.id === userId; // remove the res and call from data
})
dispatch({
type: USER_INFO,
payload: userInfo
})
})
}
find函数中的userId是一个作用域变量,因此它与您作为函数参数传递的userId不同。查找elementId的正确方法是:
const userInfo = res.find((element) =>{
return element.data.id === userId;
})