JavaScript Firebase cloudstore collections map vs. forEach

I am trying to use the map function to create an array of the items returned from a collection.

My implementation is using the forEach to iterate which works fine. However, I can't get it to work with the map function.

Here's the code:

firestore.collection("notes").doc(this.props.id).collection('items').get()
.then((snap) => {
    let items = []
    snap.forEach((doc) => {
        items.push({id:doc.id,text:doc.data().text})
        console.log(`${doc.id} => ${doc.data()}`);
    });
    console.log(items)
});
Answer:1

The forEach method exists, but not map.

However you can get an array of docs:

An array of all the documents in the QuerySnapshot.

Which you can call map on, like this:

let items = snap.docs.map(doc => {
    return { id: doc.id, text: doc.data().text }
})
Answer:2

snap may not be a true array. It's probably some sort of array-like object. Try creating a new array with the spread operator (...), then working on that, like this:

firestore.collection("notes").doc(this.props.id).collection('items').get()
.then((snap) => {
    let items = [...snap].map((doc) => {
        return {id:doc.id, text:doc.data().text}
    })
    console.log(items)
});

That should convert it to a true array which will give you the ability to use the .map function.

Answer:3

I have a jQuery plugin that does template transformation, and an Angular directive that integrates with the plugin by calling $compile after the plugin runs on the produced DOM node: //in the link ...

I have a jQuery plugin that does template transformation, and an Angular directive that integrates with the plugin by calling $compile after the plugin runs on the produced DOM node: //in the link ...

What is the difference if I use: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? numbers : ''; instead of this: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? [...

What is the difference if I use: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? numbers : ''; instead of this: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? [...

  1. what difference between
  2. what difference between can and could
  3. what difference between will and would
  4. what difference between since and for
  5. what difference between osmosis and diffusion
  6. what difference between aerobic and anaerobic respiration
  7. what difference between while and during
  8. what difference between until and by
  9. what difference between for and during
  10. what's difference between when and while
  11. what the difference when you cc an email
  12. what's the difference when cooking
  13. what time when difference
  14. what the age difference when dating
  15. what is the difference when 93 is subtracted from 387
  16. what interest rate difference when to refinance
  17. what is the difference when 6 is subtracted from 15

Can anyone help me with combing jQuery Isotope filters please. In the example (CodePen Link) there are button filters at the top, and then also a text search filter underneath. Each work ...

Can anyone help me with combing jQuery Isotope filters please. In the example (CodePen Link) there are button filters at the top, and then also a text search filter underneath. Each work ...

I want to create a Uint8Array with 8 elements. I tried this var myarr = new Uint8Array(255,255,255,255,40,92,143,2); but when I run myarr.length I get back "255" when I expect the answer would be ...

I want to create a Uint8Array with 8 elements. I tried this var myarr = new Uint8Array(255,255,255,255,40,92,143,2); but when I run myarr.length I get back "255" when I expect the answer would be ...

  1. javascript initialize uint8array