简体   繁体   English

如何使用 firebase 信息设置变量

[英]How can I set the variable with firebase information

I'm trying to set a variable with the information that i got from a get() function on firebase, but i can't.我正在尝试使用从 firebase 上的 get() function 获得的信息设置变量,但我不能。

 export default {
    name: "customer-navigation",
mixins: [navigationMixin],
components:{AppSnackBar, authModule,AuthForm},

data () {
      return {
    drawer: false,

    items: [
      { title: this.$t('navigation.home'), icon: 'home', to: '/' },
      { title: this.$t('navigation.shop'), icon: 'shopping_basket', to: 
 '/shop' },
      { title: this.$t('navigation.cart'), icon: 'shopping_cart', to: 
 '/cart' },
      { title: this.$t('navigation.orders'), icon: 'view_headline', to: 
 '/orders' },
    ],
    img_url: "" ,
    nombreFire: "",
  }
},
methods: {
  getInfo(){      

 db.collection('users').doc(authModule.state.user.uid).get().then(function(doc) {        
      this.img_url = doc.data().img_url
    })     




  }
}


}

Uncaught (in promise) TypeError: Cannot set property 'img_url' of undefined at eval, this i the error that i get Uncaught (in promise) TypeError: Cannot set property 'img_url' of undefined at eval,这是我得到的错误

Your error states that it cannot set the property img_url of undefined .您的错误表明它无法设置属性img_urlundefined Looking at your code, you're setting the property img_url for the object this .查看您的代码,您正在为 object this设置属性img_url

this is undefined because you're in a callback function. this是未定义的,因为您处于回调 function 中。 What you can do is modify your getInfo method as follows:您可以做的是修改您的getInfo方法,如下所示:


getInfo() {
  const vm = this; //this is the vue module.
  db.collection('users').doc(authModule.state.user.uid).get().then(doc => {
  vm.img_url = doc.data().img_url; //set the passed-in 'this'
});

This question can provide more insight on scoping 'this' in callbacks 这个问题可以为回调中的“this”范围提供更多见解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM