2

I'm trying to build a football office pool app and I'm having trouble saving the weekly picks. I'm using redux-saga-firebase for my calls. I've added a helper to go with it, but it's mostly just for error handling. I think my issue is figuring out the right data structure and how to update it.

export function* saveWeeklyPicks({ payload }) {
  const user = yield select(getUserProfile);
  const league = yield select(getCurrentLeague);
  const userUid = user.uid;
  const leagueUid = league.uid;

  yield call(
    callFirestore, {
      fsAction: 'updateDocument',
      // TODO: determine week dynamically
      coll: `leagues/${leagueUid}`,
      params: {
        [userUid]: {
          week1: payload.picks,
        },
      },
      successAction: t.SAVE_PICKS_SUCCESS,
      failAction: t.SAVE_PICKS_FAILURE,
    }
  );
}

I've also tried this, but it just errors (No document to update):

  coll: `leagues/${leagueUid}/${userUid}/week1`,
  params: {
      week1: payload.picks,
  }

And my helper:

export function* callFirestore({ fsAction, coll, params, successAction, failAction }) {
    try {
    const response = yield call(
            rsf.firestore[fsAction],
            coll,
            params
    );
    console.log('FIRESTORE SUCCESS RESPONSE: ', response);
    yield put({
      type: successAction,
      payload: response,
      params,
    });
  } catch (err) {
    console.log('FIRESTORE ERROR: ', err);
    yield put({
      type: failAction,
      payload: err,
    });
  }
}

So in my db, I have a leagues tables, that I access with the leagueUid, and that works fine, and the above call will replace the ${userUid}/week1 value correctly. However, it wipes out all the other data alongside it (week2, week3, etc.). What is the right way to save my picks to Firestore?

Linda Paiste
  • 38,446
  • 6
  • 64
  • 102
jmargolisvt
  • 5,722
  • 4
  • 29
  • 46

1 Answers1

0

Turns out I needed to use options: { merge: true }

    yield call(
    callFirestore, {
      fsAction: 'updateDocument',
      // TODO: determine week dynamically
      coll: `leagues/${leagueUid}`,
      params: {
        [userUid]: {
          week1: payload.picks,
        },
      },
      options: { merge: true }
      successAction: t.SAVE_PICKS_SUCCESS,
      failAction: t.SAVE_PICKS_FAILURE,
    }
  );
jmargolisvt
  • 5,722
  • 4
  • 29
  • 46