【发布时间】:2021-05-17 04:46:18
【问题描述】:
我正在尝试使用来自我的 mongo-db 领域数据库的数据填充我的 redux 存储。 每当我运行下面的函数时,它都会执行良好,但问题是数据会延迟并且最终无法到达我的 redux 存储。
我的thunk函数:
export const addItemsTest = createAsyncThunk(
"addItems",
async (config: any) => {
try {
return await Realm.open(config).then(async (projectRealm) => {
let syncItems = await projectRealm.objects("Item");
await syncItems.addListener((x, changes) => {
x.map(async (b) => {
console.log(b);
return b;
});
});
});
} catch (error) {
console.log(error);
throw error;
}
}
);
还有我的 redux reducer:
extraReducers: (builder) => {
builder.addCase(addItemsTest.fulfilled, (state, { payload }: any) => {
try {
console.log("from add Items");
console.log(payload);
state.push(payload);
} catch (error) {
console.log(error)
}
});
}
预期结果:
我的 redux 商店应该有这些数据一次 addItemsTest 返回一些东西:
[{
itemCode: 1,
itemDescription: 'Soccer Ball',
itemPrice: '35',
partition: 'partitionValue',
},
{
itemCode: 2,
itemDescription: 'Base Ball',
itemPrice: '60',
partition: 'partitionValue',
}
]
【问题讨论】:
标签: javascript typescript react-native redux redux-thunk