1

I am new in angular.

i want extract data from an observable. i do that :

validnomi(key : string): void {
  this.demandesnomiftodisplay = 
    this.demandenomisService.getSingleDemandenomis(key).subscribe(val => 

    {const civ = val[0].civ; 
     const datedemande = val[0].datedemande; 
     const nom = val[0].nom; 
     const prenom = val[0].prenom; 

     }
  );
}

my service.ts :

getSingleDemandenomis(key: string){
return this.database.list('/Demandes/DemandesBALnominatives', ref => ref.orderByKey().equalTo(key)).snapshotChanges().pipe(map(actions => {
  return actions.map(a => {
    const data = a.payload.val();
    const key = a.payload.key;
    return {key, ...data };
  });
}));
 }

But i have this error :

property prenom does not exist on type {key : string}
property nom does not exist on type {key : string}
property civdoes not exist on type {key : string}

....

3 Answers 3

2

It looks correct, you just need further to read first element of array and access needed property.

Your service:

getSingleDemandenomis(key: string): Observable<{key: string; datedemande: string}[]> {
   return this.database.list('/Demandes/DemandesBALnominatives', ref => 
       ref.orderByKey().equalTo(key)).snapshotChanges().pipe(map(actions => {
          return actions.map(a => {
              const data = a.payload.val();
              const payloadKey = a.payload.key;
              return {key: payloadKey, ...data };
          });
   }));
 }

Component:

validnomi(key : string) {
    this.demandesnomiftodisplay = this.demandenomisService.getSingleDemandenomis(key)
    .subscribe((val: {datedemande: string}[]) => console.log(val[0].datedemande));
}
Sign up to request clarification or add additional context in comments.

6 Comments

i have this error : property 'datedemande" does not exist on type {key : string}
@AlanENTEM it looks like you have some wrong types defined. Please check my edited answer for hint.
Can you please post the code for your getSingleDemandenomis method too? So I can help you with correct types.
I added some a return type for your service, and fixed shallow name issue. Try that. Please consider checking more into TypeScript, to get better familiar with types.
i have type {key : string;}[] is not asssignable to type {key:string, datedemande:string} idon't understand this problem so i can use my fonction correctly but it send me this error
|
1

The brackets from your log indicates that you are not getting an object but rather an array of object. Just get the first element of the array and then you will be able to access all the attributes of your object.

Also, you should not use JSON.stringify(), as it turns your array of object into a string.

validnomi(key : string) {
    this.demandesnomiftodisplay = this.demandenomisService.getSingleDemandenomis(key).subscribe(val => 
        // This will get the key from your object
        console.log(val[0].key);
    );
}

1 Comment

i have this error : property subscribe does not exist on type operatorfunction
1

I would stick to the observable:

validnomi(key : string) {
  this.demandesnomiftodisplay = 
    this.demandenomisService.getSingleDemandenomis(key).pipe(
    pluck('datademande')
  ).subscribe(val => console.log(val));

Here's a StackBlitz to illustrate.

1 Comment

That should be the way to go IMHO. I removed my answer as pluck seems to be a better match for the case. Get my upvote

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.