ionic2属性类型' {}'

时间:2017-07-25 11:23:25

标签: json ionic2

我在离子框架中获得了一个json in typescript json是:

{
    "result": "success",
    "user": {
        "loggedIn": true,
        "name": "Nulra",
        "password": ""
    }
}

我打印数据:

console.log("NULRA CHECKING: " + data.result + " " + data.user);

它会出错:

Typescript Error

Property 'result' does not exist on type '{}'.

Property 'user' does not exist on type '{}'.

AUTH-service.ts:

login(credentials) {
    let opt: RequestOptions;
    let myHeaders: Headers = new Headers;

    myHeaders.set('Accept', 'application/json; charset=utf-8');
    myHeaders.append('Content-type', 'application/json; charset=utf-8');
    opt = new RequestOptions({
        headers: myHeaders
    })

    return new Promise((resolve, reject) => {
        this.http.get(apiUrl+'login/0/login?email='+credentials.email+'&password='+credentials.password, opt)
        .map(res => res.json())
        .subscribe(data => {
            this.data = data;
            resolve(this.data);
        },(err) => {
            reject(err);
        });
    });
}

在login.ts中:

doLogin(){
    this.authService.login(this.loginData)
    .then(data => {
        console.log("NULRA CHECKING: " + data.result + " " + data.user);
    }
    .catch(err => {

    });
}

任何人都知道如何处理它?因为我确认的json有结果和用户。非常感谢。

当console.log(数据)时: enter image description here

2 个答案:

答案 0 :(得分:3)

试试这个 -

public userData: any = {};

doLogin(){
    this.authService.login(this.loginData)
    .then(data => {
        this.userData = data;
        console.log(`NULRA CHECKING: ${this.userData.result} ${this.userData.user}`);
    }
    .catch(err => {

    });
}

答案 1 :(得分:0)

好吧,当我简单地调用data ['result']而不是data.result时,就解决了这个问题。 对我来说,它只有在我第一次执行离子发球时才出现。