繁体   English   中英

角度4,无法从函数的内部函数访问类的变量

[英]angular 4, unable to access variable of a class from inner function of a function

我正在从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)

尝试改变

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

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

暂无
暂无

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

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