Last active
May 24, 2018 13:44
-
-
Save juliobetta/2337cfe7f42f2e9f4a31b100ebf18c27 to your computer and use it in GitHub Desktop.
Transform a Firebase.Firestore.QuerySnapshot into an iteratable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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