Javascript Ionic 2::在angular中从JSON检索属性时出现异常类型错误
我在ionic项目中创建了一个提供者,从存储器中检索用户对象,这是我的代码Javascript Ionic 2::在angular中从JSON检索属性时出现异常类型错误,javascript,json,angular,ionic2,Javascript,Json,Angular,Ionic2,我在ionic项目中创建了一个提供者,从存储器中检索用户对象,这是我的代码 import { Injectable } from '@angular/core'; import { Storage } from '@ionic/storage'; @Injectable() export class ApiProvider { public user: any; constructor( public storage: Storage ) {} getStored
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class ApiProvider {
public user: any;
constructor(
public storage: Storage
) {}
getStoredUser(){
this.storage.get('user').then(
data => {
this.user = data;
console.log(data);
}
);
return this.user;
}
当我从getStoredUser()函数中记录用户对象时,
这就是我得到的
{
authcode: "USR_65267H4390"
church_id: 2
email: "test@gmail.com"
fullname: ""An**a A***e""
id: 55
phone: "08*****7"
skills: "Football, chess, scrabble"
}
json数据正确记录,但当我尝试在不同的组件上执行此操作时
export class HomePage {
obj: any;
userObj:any = {
name:'',
email:'',
phone:'',
skills:'',
unit:'',
church_id:2 //hardcoded on every request...
};
constructor(
public navCtrl: NavController,
public api: ApiProvider,
public storage: Storage
) {}
ngOnInit():void{
this.obj = this.api.getStoredUser();
//console.log(this.obj) - outputs the object above, no errors..
//if(this.obj.length < 1) - gives me error too.. i've tried everything
if(this.obj !== null){ //------------ this line gives me error.
this.userObj = {
name: this.obj.fullname, // ------- this property gives me error.
email: this.obj.email,
phone: this.obj.phone,
skills: this.obj.skills,
unit:'',
church_id: this.obj.church_id
}
} else{
// do something else.
}
}
这是当我试图读取这个.obj的长度时得到的错误
" Uncaught(in promise): TypeError: Cannot read property
'length' of undefined TypeError: at Homepage.webpack.jsonp...
我什么都试过了,不知道该怎么办。。json对象构造正确,我需要帮助 方法
getStoredUser
返回一个值,您试图立即访问它的值
您应该在使用然后方法解析后访问返回对象:
this.api.getStoredUser().then(obj => {
this.userObj = {
name: obj.fullname, // ------- this property gives me error.
email: obj.email,
phone: obj.phone,
skills: obj.skills,
unit:'',
church_id: obj.church_id
}
});
或者,如果您与ES7兼容,请使用:
this.obj = await this.api.getStoredUser();
//if(this.obj.length < 1) - gives me error too.. i've tried everything
if(this.obj !== null){ //------------ this line gives me error.
this.userObj = {
name: this.obj.fullname, // ------- this property gives me error.
email: this.obj.email,
phone: this.obj.phone,
skills: this.obj.skills,
unit:'',
church_id: this.obj.church_id
}
}
this.obj=等待this.api.getStoredUser();
//如果(this.obj.length<1)-也给我错误。。我什么都试过了
如果(this.obj!==null){/------------这一行给出了错误。
this.userObj={
name:this.obj.fullname,//----此属性给我错误信息。
电子邮件:this.obj.email,
电话:这个,
技能:这个.obj.skills,
单位:'',
教堂标识:this.obj.church\u id
}
}
在您的ApiProvider
服务中,您将在收到响应后(异步)更新用户。从组件中调用服务中的方法,该方法立即返回用户
对象(目前为未定义
)并在以后进行更新。因此,当您从组件调用函数时,您将得到一个未定义的对象。您将收到错误,因为未定义(以及!==null,将返回true)中的对象可能与的重复
this.obj = await this.api.getStoredUser();
//if(this.obj.length < 1) - gives me error too.. i've tried everything
if(this.obj !== null){ //------------ this line gives me error.
this.userObj = {
name: this.obj.fullname, // ------- this property gives me error.
email: this.obj.email,
phone: this.obj.phone,
skills: this.obj.skills,
unit:'',
church_id: this.obj.church_id
}
}