0

My code compiles and everything is working fine, but my IDE (Visual Studio Code) shows the following error message and I'm unable to deploy my app (with ng build --prod):

ERROR in src/app/training/training.service.ts(61,46): error TS2339: Property 'name' does not exist on type '{}'. src/app/training/training.service.ts(62,50): error TS2339: Property 'duration' does not exist on type '{}'. src/app/training/training.service.ts(63,50): error TS2339: Property 'calories' does not exist on type '{}'.

I import the map operator like this:

import { map, take } from 'rxjs/operators';

and use it like this:

...
    .snapshotChanges()
    .pipe(
      map(docArray => {
        return docArray.map(doc => {
          return {
            id: doc.payload.doc.id,
            name: doc.payload.doc.data().name,
            duration: doc.payload.doc.data().duration,
            calories: doc.payload.doc.data().calories
          };
        });
      })
    )
...

My interface looks like:

export interface Exercise {
id: string;
name: string;
duration: number;
calories: number;
date?: Date;
state?: 'completed' | 'cancelled' | null;

}

What am I doing wrong?

2
  • What type does the method that uses this code returns? Commented Feb 13, 2019 at 23:26
  • It is a subscription to a firebase database/collection and fetches the data from there. I added the used interface to my question. I have the feeling that it is due to the second nested map function. Because it is not the rxjs operator, but the normal Javascript method.. Commented Feb 13, 2019 at 23:54

1 Answer 1

1

To fool Typescript, you might do this:

doc => {

    let result: any = doc.payload.doc.data();
          return {
            id: doc.payload.doc.id,
            name: result.name,
            duration: result.duration,
            calories: result.calories
          };
Sign up to request clarification or add additional context in comments.

2 Comments

That's how it works, but why do I get an error message with my version? The import is correct and I also use the pipe syntax. Does this really only have to do with my IDE?
I think it has nothing to do with your import, syntax, or IDE. The data() function might have a wrong signature, or do not signature defined in Typescript. Typescript then would not allow you to use data().id. Redefine it as any let typescript to skip type check here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.