gpt4 book ai didi

javascript - Angular 4,无法从函数的内部函数访问类的变量

转载 作者:行者123 更新时间:2023-12-01 02:29:03 25 4
gpt4 key购买 nike

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

我想将数据保存到该变量以在用户界面中加载数据。

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) => {

关于javascript - Angular 4,无法从函数的内部函数访问类的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48460954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com