Home > Enterprise >  Argument of type 'void' is not assignable to parameter of type 'SetStateAction<nev
Argument of type 'void' is not assignable to parameter of type 'SetStateAction<nev

Time:05-08

I want to implement a code to retrieve data from firestore collection

I use typescript as a programming language. But found an error while following the existing example, while the example uses javascript

the error occured at setBookmark(querySnapshot.forEach((doc) => {

React.useEffect(() => {
    const getBookmark = async () => {
      try {
        const querySnapshot = await getDocs(collection(db, "bookmark"));
        setBookmark(querySnapshot.forEach((doc) => {
          id: doc.id,
          data: doc.data()
          // console.log(`${doc.id} => ${doc.data()}`);
        }));
      } catch (e) {
        console.error("Error getting document: ", e);
      }
    };

    getBookmark();
  }, []);

CodePudding user response:

forEach returns void hence the error. I think you want to use .map instead. Your current would always set setBookmark(null). I would write the code for you but its not clear what goes into bookmark.

map will return a new list based on the old list while forEach is to just iterate and not returning anything.

CodePudding user response:

As I remember, you can't use map with querySnapshot, but you can create an array from it.

const myArray = [];
querySnapshot.forEach((doc) => {
  myArray.push({
    id: doc.id,
    data: doc.data()
  })
setBookmark(myArray)
  • Related