我从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)
答案 0 :(得分:2)
尝试更改
docRef.get().then(function(doc) {
到
docRef.get().then((doc) => {