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)
});
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 }
})
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.
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) ? [...
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 ...