Simon Pintz
Simon Pintz

Reputation: 1

How to add doc and stores its id immediately somewhere else in firebase? using react native

I'm trying to create a doc in my albums collection and then take the id of that new doc created and store it in other places in my firestore. Here's my code and my error message.

const createAlbum = () => {
firebase
  .firestore()
  .collection("albums")
  .add(albumName)
  .then((snapshot) => {
    let newAlbum = snapshot.docs.map((doc) => {
      const id = doc.id;
      return id;
    });
    for (let i = 0; i < usersSelected.length; i++) {
      firebase
        .firestore()
        .collection("following")
        .doc(usersSelected[i].id)
        .collection("userFollowing")
        .doc(newAlbum.id)
        .set({});
    }
  });

};

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'snapshot.docs.map')] at [native code]:null in flushedQueue at [native code]:null in callFunctionReturnFlushedQueue

Upvotes: 0

Views: 563

Answers (1)

Peter Obiechina
Peter Obiechina

Reputation: 2835

Try something like this:

const createAlbum = () => {
  firebase
    .firestore()
    .collection("albums")
    .add(albumName)
    .then((doc) => {
      const id = doc.id;
      for (let i = 0; i < usersSelected.length; i++) {
        firebase
          .firestore()
          .collection("following")
          .doc(usersSelected[i].id)
          .collection("userFollowing")
          .doc(id)
          .set({ });
      }
    });
}

Upvotes: 1

Related Questions