首页 > 解决方案 > 从 mapStateToProps 传递 Prop 到 Dispatch Action 是未定义的

问题描述

我正在尝试使用 mapStateToProps 中的值调度操作。错误说:

FirebaseError:使用无效数据调用函数 DocumentReference.update()。不支持的字段值:未定义(在字段 xp 中找到)

在主文件中设置:

componentWillReceiveProps(){
    this.props.updateInLobby({ 
        userid: this.props.auth, 
        xp: this.props.user.xp,
        lobby: this.props.match.params.id,
}
...
const mapStateToProps = (state, ownProps) =>{
    return{
        user: state.firebase.profile,
        auth: state.firebase.auth.uid,
    }
}
 const mapDispatchToProps = (dispatch) =>{
    return{
        updateInLobby: (data) => {dispatch(updateInlobby(data))},
    }
}
export default compose(
     connect(mapStateToProps, mapDispatchToProps),
    firestoreConnect((props)=>{
    return[
        {collection: "lobby@"+props.match.params.id}]
    })
)(RootLobby);

调度行动文件:

export const updateInlobby = (data) =>{
    return(dispatch, getState, {getFirestore}) =>{
        const firestore = getFirestore();
        firestore.collection("lobby@"+data.lobby).doc(data.userid).update({
            xp: data.xp
        })
    }

为什么 userid 和 lobby 似乎被定义为文档引用正在工作,但 xp 未定义?

标签: reactjsfirebasereact-reduxreact-props

解决方案


推荐阅读