so im creating instagram clone and in the firebase docs trying to update the fields:
const uploadPostOnline = (imageUrl, caption) => {
const userRef = doc(db, "users", "posts");
setDoc(
userRef,
{ imageUrl: imageUrl },
{ caption: caption },
{ likes: 0 },
{ likesByUsers: [] },
{ comments: [] },
{ createdAt: serverTimestamp() },
{}
);
};
this is my firebase database picture:
I want my code to create a collection of "post" for every user with the fields i added.
EDITED: This is the screenshot i got after adding catch
Security Rule Code:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function userIsAuthenticated(){
return request.auth != null;
}
match /{path=**}/posts/{postId}{
allow read, write : if userIsAuthenticated();
}
match /users/{userId}{
allow read, write : if userIsAuthenticated();
}
}
}
2
Answers
The
setDoc()
takes a DocumentReference as first parameter and a single object containing all the fields of your document as second. Try refactoring the code as shown below:You need to use the ID of the user to add a
post
document in a subcollection, as follows: