nisino25
nisino25

Reputation: 87

Saving the data from firestore

I don't know how to save the data from the firestore, I tried to save it with this.favLists so I don't have to send requests everytime when I send the request.

export default defineComponent({
 data() {
  return{
   favLists: '',
  }
 },
 methods: {
  async getFavorite() {
  var docRef = 
   db.collection("userFavorites").doc(this.currentUser.uid);
   docRef.get().then(function(doc) {
      if (doc.exists) {
        this.favLists = doc.data()
        console.log(doc.data())
        console.log(this.favLists)
      } else {
        console.log("No such document!");
      }
  }).catch(function(error) {
    console.log("Error getting document:", error);
  });
},

Upvotes: 0

Views: 30

Answers (1)

Harshana
Harshana

Reputation: 5496

It seems like there is an issue with the scope. Try replacing callback functions with arrow functions:

export default defineComponent({
 data() {
  return{
   favLists: '',
  }
 },
 methods: {
  async getFavorite() {
  var docRef = 
   db.collection("userFavorites").doc(this.currentUser.uid);
   docRef.get().then((doc) => {
      if (doc.exists) {
        this.favLists = doc.data()
        console.log(doc.data())
        console.log(this.favLists)
      } else {
        console.log("No such document!");
      }
  }).catch((error) => {
    console.log("Error getting document:", error);
  });
},

Upvotes: 2

Related Questions