0

Data Array(trytArr) look that: https://i.stack.imgur.com/afxUI.png

I try map array from state:

  {
                trytArr.map((item, i) => {
                  return (
                    <Hehe
                      key={i}
                      Procent={item.Procent}
                      />
                  );
                })
     }

I get: TypeError: undefined is not a function (near '...dopasowanieArr.map...')

1 Answers1

0

According to your screenshot of dopasowanieArr, it is an Object.

Then, you're trying to use map() on the Object. map() can only be used on collections, like Arrays.

Judging by the name of it (dopasowanieArr), you're expecting it to be an array, so you can convert the data you're getting to an array using the following (assuming that your data is returning a single object like your screenshot shows):

const data = documentSnapshot.data()
setDopasowanieArr(
  Object.keys(data).map(key => data[key])
);

If you really are expecting it to be an array, I'd probably also consider starting with useState([]) instead of useState() since you already have a loading state as well (which should probably be useState(true) instead of useState("true") as you currently have.

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Amazing <3 but props doesn't works: Procent={item.Procent} tested with: Procent="smth" works. – Sam WieszKto Feb 02 '21 at 21:18
  • Look at what your object has in it -- Procent is inside another key (I can't copy and paste it since you included an image and not text). So, it would be something like asideasdfasdf.Procent. – jnpdx Feb 02 '21 at 21:52
  • ok, but I want to foreach element with ex. "ppN2vRyyffVMMbK5YB2iv67PWiG2" not object which we add to array in: setDopasowanieArr([documentSnapshot.data()]); https://i.imgur.com/E78SsJd.png – Sam WieszKto Feb 02 '21 at 22:06
  • Okay, then you need to turn your `Object` into an array based on its keys/values: https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript I have edited my answer to reflect this. – jnpdx Feb 02 '21 at 22:07
  • By the way, it seems like you have a mismatch between what your query is getting and what you expect to see. You keep expecting an array, but your query is returning a single document. You might need to look into the logic you're using for your query and how it relates to your Firestore structure – jnpdx Feb 02 '21 at 22:13
  • Amazing <3 Thank you very much <3 – Sam WieszKto Feb 02 '21 at 22:30