【问题标题】:angular 4, unable to access variable of a class from inner function of a function角度 4,无法从函数的内部函数访问类的变量
【发布时间】:2018-01-26 11:58:46
【问题描述】:

我正在从 firebase firestone 获取数据,数据已成功检索并在控制台中打印。但我无法将数据保存到全局变量中。 此错误恰好位于第 37 行

我想将数据保存到该变量以在 ui 中加载数据。

24 thisaddress = {};
25  ngOnInit() {
26    this.data = new AddressserviceService();
27    this.sub = this.route.params.subscribe(params => {
28      this.id = params['id'];
29   });
30   
31   this.newvisitordb = AddressserviceService.getdb();
32   var docRef = this.newvisitordb.collection("addresses").doc(this.id);
33   docRef.get().then(function(doc) {
34        if (doc.exists) {
35          console.log("Document data:", doc.data());
36          //**this line is not working**
37          this.thisaddress= doc.data();
38        } 
39    }).catch(function(error) {
40        console.log("Error getting document:", error);
41    });

第 37 行出错

错误是

>     Error getting document: TypeError: Cannot set property 'thisaddress' of undefined
>         at eval (newvisitor.component.ts:45)
>         at ZoneDelegate.invoke (zone.js:388)
>         at Object.onInvoke (core.js:4733)
>         at ZoneDelegate.invoke (zone.js:387)
>         at Zone.run (zone.js:138)
>         at eval (zone.js:858)
>         at ZoneDelegate.invokeTask (zone.js:421)
>         at Object.onInvokeTask (core.js:4724)
>         at ZoneDelegate.invokeTask (zone.js:420)
>         at Zone.runTask (zone.js:188)

【问题讨论】:

  • 这个问题已经回答了here

标签: javascript angular firebase google-cloud-firestore


【解决方案1】:

尝试改变

docRef.get().then(function(doc) {

docRef.get().then((doc) => {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2018-06-23
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多