0

The array doses is the one that I wanted to retrieve, however I keep getting an error that says that map is not a function. This is what it looks like in firebase:

enter image description here

codes to retrieve firestore data:

  useEffect(() => {
    const unsubscribe = firestore
      .collection("users")
      .doc(scanResult)
      .onSnapshot((snapshot) => {
        const arr = [];
        arr.push({
          ...snapshot.data(),
        });

        setUsers(arr);
      });

    return () => {
      unsubscribe();
    };
  }, []);

mapping the users array:

 {users && users.map((user) => (
      <li>{user.email}</li> // I could already retrieve the data that are not inside the doses array

     {/* get the doses array does not work */}
                  {users &&
                    users.doses.map((index) => {
                      <li>{index.selectedVaccine}</li>;
                    })}
    ))}

1 Answer 1

1

Your doses field is not an array, but a map/object. An array has numeric, sequential keys, while a map/object has string keys.

If you want to loop over the keys of the field, you can do:

Object.keys(users.doses).map((key) => {
  <li>{user.doses[key]}</li>;
})}

If you instead just want to display the selectedVaccine value of the doses field, you'd do:

<li>{user.doses.selectedVaccine}</li>;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.