How can I get all the documents inside the subcollection "userPosts" in Firebase Firestore with JavaScript? You can see my database structure in the pictures below.
Here is what I have tried:
firebase.auth().onAuthStateChanged(function(user) {
var postRef = database.collection('posts').doc().collection('userPosts');
postRef.get().then(snapshot => {
setupPosts(snapshot.docs)
})
const posts = document.querySelector('.posts');
const setupPosts = (data) => {
let html = '';
data.forEach(doc => {
const post = doc.data();
console.log(post)
const li = `
<li>
<div class="title">${post.title}</div>
<div class="content">${post.content}</div>
</li>
`;
html += li
})
posts.innerHTML = html;
}
})
What this code should do is to get the documents in the subcollection "userPosts". And it should go into every document in the collection "posts" and then go into "userPosts" and get every document in that subcollection.