In my case, I was facing this error because of using same action names of asyncThunk. ("orders/fetchOrders") in both functions
First I will show the mistake I was doing is following, check the action names. Both are same mistakenly.
export const fetchOrders = createAsyncThunk("orders/fetchOrders", async () => {
try {
const querySnapshot = await getDocs(
query(collection(db, "orders"), orderBy("createdAt", "desc"))
);
const orders = querySnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
console.log(orders);
return orders;
} catch (error) {
return Promise.reject(error);
}
});
export const updateOrderStatus = createAsyncThunk(
"orders/fetchOrders",
async ({ orderId, newStatus }) => {
try {
console.log(orderId, newStatus);
const orderRef = doc(db, "orders", orderId);
await updateDoc(orderRef, {
status: newStatus,
updatedAt: Timestamp.now().toDate(),
});
toast.success("Order status updated successfully");
} catch (error) {
toast.error(error.message);
}
}
);
Just keep the action names different
export const fetchOrders = createAsyncThunk("orders/fetchOrders", async () => {
try {
const querySnapshot = await getDocs(
query(collection(db, "orders"), orderBy("createdAt", "desc"))
);
const orders = querySnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
console.log(orders);
return orders;
} catch (error) {
return Promise.reject(error);
}
});
export const updateOrderStatus = createAsyncThunk(
"orders/updateOrderStatus",
async ({ orderId, newStatus }) => {
try {
console.log(orderId, newStatus);
const orderRef = doc(db, "orders", orderId);
await updateDoc(orderRef, {
status: newStatus,
updatedAt: Timestamp.now().toDate(),
});
toast.success("Order status updated successfully");
} catch (error) {
toast.error(error.message);
}
}
);