/    Sign up×
Community /Pin to ProfileBookmark

Unable to fetch the data from firebase child using React Redux-Saga?

I am unable to fetch firebase child data with React and Redux-Saga. Can you please check the below code ? Why it is not working with redux-saga.

import { takeLatest, put, call, fork, all, take } from “redux-saga/effects”;
import { getContactsSuccess, getContactsFail } from “./actions”;
import * as types from “./actionTypes”;
import firebaseDb from “../firebase”;

export function* onGetContactsStartSync() {
try {
const snapshot = yield firebaseDb.child(“contacts”).on(“value”);

yield put(getContactsSuccess(snapshot.val()));
console.log(“snapshot”, snapshot);

} catch (error) {
yield put(getContactsFail(error));
}
}

export function* onLoadContacts() {
yield takeLatest(types.GET_CONTACTS_START, onGetContactsStartSync);
}

const contactSagas = [fork(onLoadContacts)];

export default function* rootSaga() {
yield all([…contactSagas]);
}

When I tried with redux-thunk then I was able to fetch the data from firebase with the below approach

export const getContacts = () => {
return function (dispatch) {
dispatch(getContactsStart());
firebaseDb.child(“contacts”).on(“value”, (snapshot) => {
try {
if (snapshot.val() !== null) {
dispatch(getContactsSuccess(snapshot.val()));
} else {
dispatch(getContactsSuccess({}));
}
} catch (error) {
dispatch(getContactsFail(error));
}
});
};
};

I am unable to fetch firebase child data with React and Redux-Saga. Can you please check the below code ? Why it is not working with redux-saga.

import { takeLatest, put, call, fork, all, take } from “redux-saga/effects”;
import { getContactsSuccess, getContactsFail } from “./actions”;
import * as types from “./actionTypes”;
import firebaseDb from “../firebase”;

export function* onGetContactsStartSync() {
try {
const snapshot = yield firebaseDb.child(“contacts”).on(“value”);

yield put(getContactsSuccess(snapshot.val()));
console.log(“snapshot”, snapshot);

} catch (error) {
yield put(getContactsFail(error));
}
}

export function* onLoadContacts() {
yield takeLatest(types.GET_CONTACTS_START, onGetContactsStartSync);
}

const contactSagas = [fork(onLoadContacts)];

export default function* rootSaga() {
yield all([…contactSagas]);
}
When I tried with redux-thunk then I was able to fetch the data from firebase with the below approach

export const getContacts = () => {
return function (dispatch) {
dispatch(getContactsStart());
firebaseDb.child(“contacts”).on(“value”, (snapshot) => {
try {
if (snapshot.val() !== null) {
dispatch(getContactsSuccess(snapshot.val()));
} else {
dispatch(getContactsSuccess({}));
}
} catch (error) {
dispatch(getContactsFail(error));
}
});
};
};
So, what is going wrong with redux-saga approach ? I wanted to replicate the same thing with redux-saga.

I think i am missing some small thing here, which is might be very basic. Let me know if have some solution.

I tried the below approach as well.

function createContactChannel() {
const listener = eventChannel((emit) => {
firebaseDb.child(“contacts”).on(“value”, (data) => {
if (data.val() !== null) {
emit(data.val());
}
});
return () => firebaseDb.child(“contacts”).off(listener);
});

console.log(“listener”, listener);
return listener;
}

export function* onGetContactsStartSync() {
try {
const contacts = createContactChannel();

yield put(getContactsSuccess(contacts));

} catch (error) {
yield put(getContactsFail(error));
}
}

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @vishalvickram1991 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...