Skip to content

Instantly share code, notes, and snippets.

@juliobetta
Last active May 24, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliobetta/2337cfe7f42f2e9f4a31b100ebf18c27 to your computer and use it in GitHub Desktop.
Save juliobetta/2337cfe7f42f2e9f4a31b100ebf18c27 to your computer and use it in GitHub Desktop.
Transform a Firebase.Firestore.QuerySnapshot into an iteratable
function fetchAll() {
return firebase.firestore().collection('objects').get().then(snapshot => (
{
[Symbol.iterator]: function iterator() {
const { docs } = snapshot;
return {
next() {
return {
done: docs.length === 0,
value: docs.length && docs.shift().data()
};
}
};
},
raw: snapshot,
forEach: snapshot.forEach,
map: fn => docs.map(doc => fn(doc.data()))
}
))
}
fetchAll().then(response => {
// transform it into an array
const array = Array.from(response);
// spread in an array
const state = [/* lots of objects here */];
const newState = [...state, ...response];
// in a for..of loop
for(doc of response) {
console.log(doc);
}
// map function
response.map(doc => console.log(doc))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment