如何使用异步thunk和redux切片从Firebase登录正确返回用户对象?

huangapple go评论81阅读模式
英文:

How do I properly return a user object from firebase sign using an async thunk and redux slice?

问题

在登录后,我想将用户对象返回给切片,但我一直收到关于不可序列化数据的错误,我该如何正确配置它?<br>

export const updateProfileAsync = createAsyncThunk('user/updateProfile', async (user: any, profile: any) => {
    const response = await updateProfile(user, profile);
    return response.user;
});

接口:

interface userState {
    user: User;
}

在我的切片中:

extraReducers: (builder) => {
    builder.addCase(loginAsync.pending, (state, action) => {
        console.log('pending');
        state.loading = 'pending';
    });
    builder.addCase(loginAsync.fulfilled, (state, action) => {
        state.loading = 'succeeded';
        console.log('logged in');
        console.log(action.payload);
        state.loginError = '';
    });
    builder.addCase(loginAsync.rejected, (state, action) => {
        state.loginError = action.error.message;
        state.loading = 'failed';
        console.log('failed');
    });
}

如何使用异步thunk和redux切片从Firebase登录正确返回用户对象?

英文:

After sign in, I wan to return the user object to the slice, but I keep getting an error, about non serializable data, how do I properly configure this ?<br>
如何使用异步thunk和redux切片从Firebase登录正确返回用户对象?

export const updateProfileAsync = createAsyncThunk(&#39;user/updateProfile&#39;, async(user:any, 
profile:any)=&gt;{
const response = await  updateProfile(user,profile)

return response.user
})

interface

interface userState {
user:User 
}

in my slice,

extraReducers:(builder) =&gt; {
    builder.addCase(loginAsync.pending, (state,action)=&gt;{
        console.log(&#39;pending&#39;)
        state.loading = &#39;pending&#39;
    })
    builder.addCase(loginAsync.fulfilled, (state,action)=&gt;{
        state.loading = &#39;succeeded&#39;
        console.log(&#39;logged in&#39;)
        console.log(action.payload)
        state.loginError = &#39;&#39;
    })  
    builder.addCase(loginAsync.rejected, (state,action)=&gt;{
        state.loginError = action.error.message
        state.loading = &#39;failed&#39;
        console.log(&#39;failed&#39;)
    })

答案1

得分: 1

你的 user.user 对象似乎不可序列化,但你返回给 slice 的数据应该是可序列化的。<br>

你可以在这里查看如何在 Redux 中处理不可序列化的数据。<br>

还可以参考与 React-Redux-Firebase 一起使用

英文:

It seems that your object user.user is not aserializable, but data that you return to the slice should be aserializable.<br>

you can have a look here how to work with Non-Serializable data in Redux.<br>
see also Use with React-Redux-Firebase

huangapple
  • 本文由 发表于 2023年7月3日 08:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定