When I call the function it will [object Promise] instead of data, I think the promise will be in a pending state, and it returns. Can anyone help me with how to do this in the right way?
Here is my util function.
export const GetDataFromSnapshot = async (snapshot) => {
const data = [];
try {
return await snapshot.onSnapshot((docs) => {
const currentState = [];
docs.forEach((doc) => {
currentState.push(doc.data());
});
data.push(currentState);
console.log(currentState);
// 0:
// id: "71u638UFCpGs6hLkWP"
// timestamp: t {seconds: 2177433000, nanoseconds: 0}
// title: "Remind Me in future"
});
} catch (error) {
console.log(error);
}
};
and this function called by saga-watcher generator function will look like this.
// TODO: action calling the API
function* fetchReminder() {
try {
const reminders = yield call(GetAllReminders);
const resultDoc = GetDataFromSnapshot(reminders);
console.log(`resultDoc =>> ${resultDoc}`);
// resultDoc =>> [object Promise]
resultDoc
.then((data) => {
console.log(`resultDoc =>> ${resultDoc}`);
// resultDoc =>> [object Promise]
console.log(`data =>> ${data}`);
// data =>> function () {
// i.Zl(), r.cs.ws(function () {
// return Pr(r.q_, o);
// });
// }
data.json();
// Unhandled Rejection (TypeError): data.json is not a function
})
.then((response) => console.log(`response =>> ${response}`));
yield put(setReminder(reminders));
how to get the data will look like this. An array of objects from the firestore database
0:
id: "71u638UFCpGs6hLkWP"
timestamp: t {seconds: 2177433000, nanoseconds: 0}
title: "Remind Me in future"