简体   繁体   中英

Property 'pname' does not exist on type '{}'

I am working on an ionic app i am using firebase connectivity for it. I got my json from firebase but it giving me error in iterating it. my provider.ts is as follow

export class MyProvider {
   masterdata : Array<any>;

   data : Array<any>;
  constructor(public afd: AngularFireDatabase) {
   }

 getDataByPage() {
 return this.afd.list('/masterdata/0/page/').valueChanges();
 }
}

i want to pass it to diff pages my home.ts is

export class Home {
   home:Array<any>;
   data:any;
  constructor(public navCtrl: NavController, public navParams: NavParams, public popoverCtrl: PopoverController, public modalCtrl: ModalController,public masterdata: MyProvider ) {



    this.masterdata.getDataByPage().subscribe(result => {

    for(let i in result )
    {
        console.log(result[i]);
        if(result[i].pname == "Home")
        {
            this.data = result[i];
        }
    }

    });

  }
}

My json is

 this.masterdata= {
    page: [{
        "pname": "Home",
        "pagetitle": "Most Recent",
        "list": [{
            img: 'assets/img/01.png',
            isAnimated: false
        }, {
            img: 'assets/img/02.png',
            isAnimated: false
        }, {
            img: 'assets/img/02.png'
        }, {
            img: 'assets/img/03.png'
        }]
    },
    {
        "pname": "Profile",
        "img": "assets/img/user-img.png",
        "name": "Justin Timberlake",
        "email": "Justin Timberlake@yahoo.com"
    }, ]
}

it giving me error as following

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

This should work result [i]['pname'] .

Next try

let data = JSON.parse(result);
for(let i in data )
{
   console.log(data [i]);
   if(data [i].pname === "Home")
   {
     this.data = data [i];
   }
 }

Normal JSON looks like:

page: [{
    pname: "Home",
    pagetitle: "Most Recent",

This below condition you can change with Rxjs map operator. Check the response you are getting in console and update the logic accordingly. But make sure you have imported RxJS library.

this.masterdata.getDataByPage().subscribe(result => res.json()).map(result =>{

  console.log(res);
  if(res.pname == "Home")
  {
    this.data = res;
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM