Cannot read property 'ready' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'ready' of undefined
Cannot read property 'ready' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'ready' of undefined
我正在尝试访问 firebase onAuthStateChanged() 函数中的本地存储值。取决于我想重定向用户。但是我收到了这个错误。当我检查 firebase onAuthStateChanged() 函数之外的存储值时,它工作正常。谁能告诉我为什么我无法访问 onAuthStateChanged() 函数中的离子存储值?
提前致谢
home.page.ts
constructor(public storageService : StorageService){}
ngOnInit() {
this.validate()
}
async validate(){
this.afauth.auth.onAuthStateChanged( async function(user) {
if (user) {
// User is signed in.
await this.storageService.ready()
const name = await this.storageService.get('name')
const business_name = await this.storageService.get('business_name')
} else {
// No user is signed in.
}
});
}
storageservice.ts
constructor( private storage: Storage) {
}
async get(key: string): Promise<any> {
try {
const result = await this.storage.get(key);
console.log('storageGET: ' + key + ': ' + result);
if (result != null) {
return result;
}
return null;
} catch (reason) {
console.log(reason);
return null;
}
}
async ready() : Promise<any>{
try {
this.storage.ready();
return true;
}
catch(err) {
console.log(err)
}
}
在您的代码中,您具有以下内容:
this.afauth.auth.onAuthStateChanged( async function(user) {
if (user) {
// User is signed in.
await this.storageService.ready()
const name = await this.storageService.get('name')
const business_name = await this.storageService.get('business_name')
} else {
// No user is signed in.
}
});
你应该改成这样:
this.afauth.auth.onAuthStateChanged( async ((user) => {
if (user) {
// User is signed in.
await this.storageService.ready()
const name = await this.storageService.get('name')
const business_name = await this.storageService.get('business_name')
} else {
// No user is signed in.
}
});
使用使用词法范围的箭头函数,这意味着您将能够读取在该函数之外定义的变量。
我正在尝试访问 firebase onAuthStateChanged() 函数中的本地存储值。取决于我想重定向用户。但是我收到了这个错误。当我检查 firebase onAuthStateChanged() 函数之外的存储值时,它工作正常。谁能告诉我为什么我无法访问 onAuthStateChanged() 函数中的离子存储值?
提前致谢
home.page.ts
constructor(public storageService : StorageService){}
ngOnInit() {
this.validate()
}
async validate(){
this.afauth.auth.onAuthStateChanged( async function(user) {
if (user) {
// User is signed in.
await this.storageService.ready()
const name = await this.storageService.get('name')
const business_name = await this.storageService.get('business_name')
} else {
// No user is signed in.
}
});
}
storageservice.ts
constructor( private storage: Storage) {
}
async get(key: string): Promise<any> {
try {
const result = await this.storage.get(key);
console.log('storageGET: ' + key + ': ' + result);
if (result != null) {
return result;
}
return null;
} catch (reason) {
console.log(reason);
return null;
}
}
async ready() : Promise<any>{
try {
this.storage.ready();
return true;
}
catch(err) {
console.log(err)
}
}
在您的代码中,您具有以下内容:
this.afauth.auth.onAuthStateChanged( async function(user) {
if (user) {
// User is signed in.
await this.storageService.ready()
const name = await this.storageService.get('name')
const business_name = await this.storageService.get('business_name')
} else {
// No user is signed in.
}
});
你应该改成这样:
this.afauth.auth.onAuthStateChanged( async ((user) => {
if (user) {
// User is signed in.
await this.storageService.ready()
const name = await this.storageService.get('name')
const business_name = await this.storageService.get('business_name')
} else {
// No user is signed in.
}
});
使用使用词法范围的箭头函数,这意味着您将能够读取在该函数之外定义的变量。