Cannot reach property of object AngularFire2/Typescript

Kode Ch

I added a search bar to my project, and took the code from ionic docs

 getItems(ev: any) {

    this.initializeItems();

    const val = ev.target.value;

    if (val && val.trim() != '') {
      this.items = this.items.pipe(filter(item => {
        console.log(item);
        let t = item.title;
        return (t.toLowerCase().indexOf(val.toLowerCase()) > -1);
      }))
    }
  }

This is how I'm initializing the items

initializeItems(){
    this.items = this.newslist;
  }

And in the constructor

this.newslist = afDB.list('information/news', (ref) => ref.orderByChild('inverteddatetime')).snapshotChanges().pipe(
        map(actions => 
          actions.map(a => ({ key: a.key, ...a.payload.val() }))
        )
      );

The error that I'm getting is at let t = item.title; where "title" is underlined red with the message

Property "title" does not exist on type {}

When I log item in console.log(item); I get the correct results back though with their appropriate properties:

    (2) [{…}, {…}] 
0:{
datetime:"Saturday, June 23, 2018 12:28 AM"
image:"data:image/jpeg;base64,/9j/4..."
inverteddatetime:-1529703181246
key:"-LFdPXgxgAQA1RVnGmMD"
subtitle:"Water can cause problems, be wary of the things."
text:"Water quality is a subject that’s been big news lately. Residents of Flint, Michigan are suffering from toxic levels of lead in ..."
title:"12 Toxins in Your Drinking Water"
}

1 : {key: "-LFXmks2UICxqTNkicNv", datetime: "Thursday, June 21, 2018 5:35 PM",  subtitle: "Unfortunate problems", …} length : 2
    __proto__ : Array(0)

Data Structure: Firebase Data Structure (I don't have enough reputation to display it on the thread)

I have a simple question, I don't understand why I can't reach the property "title" of the item although when I log it it shows all items and their properties. Is it because it's returning JSON and I should map it to an array?

Sergey Rudenko

This is type error that occurs since typescript is not “aware” of your object’s (“item” in this case) data shape. Normally you would want to type it and thus resolve it, like this:

let t = (item as any).title

But in your case here it seems like you are trying to apply filter similarly how someone did that in this issue: Observable of array with filter using Angular 5 with RxJS

Your solution should be 1:1 similar: apply .map onto the observable you have (this.items.map( etc ) )

Like this:

getItems(ev: any) {

    this.initializeItems();

    const val = ev.target.value;

    if (val && val.trim() != “”) {
      this.items = this.items.map( items => items.filter( item => :::your filter condition here:::))
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error while sorting array of objects Cannot assign to read only property '2' of object '[object Array]'

Angular2/Typescript/ngRx - TypeError: Cannot assign to read only property of object

Cannot read property of object

Cannot delete object property in Javascript

Typescript - Conditional property of object

Cannot read property ... of undefine , at Object.callbackFromNative In angular 2

Typescript object property typings

Javascript property cannot reference property of the same object

Cannot sum property of object

Angularfire2. Cannot read property 'subscribe' of undefined

Ngrx : Cannot assign to read only property 'Property' of object '[Object]'

Cannot access object property values in Typescript

Typescript is Type for object property

'property' object cannot be intepreted as an integer

cannot reach EC2 instance in ECS cluster

Typescript object with a known property

cannot access inherited property in object

Cannot reach site via local network to Apache2 server

Cannot access property object in Angular.io TypeScript component

Angular 2 ngModel cant reach Array property

Cannot read property object reactJS

Cannot access object property

Cannot access value of object property

TypeError: Cannot assign to read only property '0' of object '[object Array]' in typescript

Omit an object property in typescript

Cannot access an Object's property

Powershell: Nested Property, The property cannot be found on this object

Access Object property in Typescript

Cannot access nested property from an array of object TypeScript