I have an admin area which collects all data from Firebase in an array. My task is to update data and send it to Firebase for specific users (I need to put some comment). Problem is that in .doc('id of document')
, I don't know how to get the specific ID of document in Firebase. Function works fine if I put in an ID of a specific document (e.g. "2jzm4AcWTVNIlT9ESH7V"
). doc.data()
returns all the data from Firebase and each ID is stored in the object together with the data.
<script>
import moment from 'moment'
export default{
// ...
mounted(){
db.collection("form").where("posted_at", ">=", 1)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc=> {
console.log(doc.id, " => ", doc.data());
this.array.push(Object.assign({}, doc.data(), {id: doc.id}));
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
},
methods:{
comment(){
let id=this.array.id;
db.collection("form")
.doc(id)
.update({
comment: this.comment1 //data(){return{comment1:''}}
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
}
}
};
</script>
P.S. i get this error for function:
FirebaseError: [code=invalid-argument]: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined
The line
let id=this.array.id;
is trying to get theid
property of the array itself, not the ID of an entry in the array.Based on the YouTube tutorial you are following, when you attach your
comment()
method in thev-for
loop, you should pass the array entry it relates to in as a parameter. When this method is invoked, it should set the ID that is being edited, load the existing comment (if it exists) and open the modal dialog to edit the comment.I have edited, proofed and reworked the code you provided into this updated file. It was written free hand so let me know if there are any bugs.
A summary of the changes:
editComment(userDetails)
andsaveComment()
event handlersformatTime
filter from your other questiondiv
- shouldn't have been insidev-for
loop