1

I'm following this Tutorial for my Ionic app. This using http Module.But now angular 4+ has httpClient. I have done some changes according to that. Now app is working fine until I start. When I restart the app it shows the error

Property 'questions' does not exist on type 'Object'.

but again when I hit ctrl+s and save changes error is disappeared. How can I fix this??

Json is

 {
    "questions": [

        {
            "flashCardFront": "<img src='assets/imgs/helicopter.png' />",
            "flashCardBack": "Helicopter",
            "flashCardFlipped": false,
            "questionText": "What is this?",
            "answers": [
                {"answer": "Helicopter", "correct": true, "selected": false},
                {"answer": "Plane", "correct": false, "selected": false},
                {"answer": "Truck", "correct": false, "selected": false}
            ]
        },
        {
            "flashCardFront": "<img src='assets/imgs/plane.png' />",
            "flashCardBack": "Plane",
            "flashCardFlipped": false,
            "questionText": "What is this?",
            "answers": [
                {"answer": "Helicopter", "correct": false, "selected": false},
                {"answer": "Plane", "correct": true, "selected": false},
                {"answer": "Truck", "correct": false, "selected": false}
            ]
        },
        {
            "flashCardFront": "<img src='assets/imgs/truck.png' />",
            "flashCardBack": "Truck",
            "flashCardFlipped": false,
            "questionText": "What is this?",
            "answers": [
                {"answer": "Helicopter", "correct": false, "selected": false},
                {"answer": "Plane", "correct": false, "selected": false},
                {"answer": "Truck", "correct": true, "selected": false}
            ]
        }
    ]
}

Here is my code in provider using httpClient

load() {
    if(this.data) {
        return Promise.resolve(this.data);
    }

    return new Promise(resolve => {
        this.http.get('assets/data/questions.json').subscribe(data => {
            this.data = data.questions; //error here
            resolve(this.data);
        });
    });
 }
2
  • 1
    Can you add the json to the question ? Commented Jun 19, 2018 at 5:44
  • @Anuradha Gunasekara done Commented Jun 19, 2018 at 6:46

3 Answers 3

2

Hi you have to add latest http into your project FIDDLE DEMO

npm install @angular/http@latest --save

Next, open and edit 'src/app/app.module.ts' then add this import.

imports: [
   BrowserModule,
   HttpClientModule,
   IonicModule.forRoot(MyApp)
],

Next add http into your .ts file

import { HttpClient } from '@angular/common/http';

create variable in constructor

constructor(public http: HttpClient) {
     console.log('Hello RestServiceProvider Provider');
}

Then use it

load() {
  if(this.data) {
      return Promise.resolve(this.data);
  }

  return new Promise(resolve => {
      this.http.get('https://jsonplaceholder.typicode.com/posts/1').subscribe(data => {
          this.data = data //error here
          alert(JSON.stringify(data));
          resolve(this.data);
      });
  });
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know need to add a constructor to use any module
Check the fiddle demo and you can just replace your file and check it will work for you or not
npm install @angular/http@latest --save not required at all see from which package you are importing HttpClient -> '@angular/common/http';
1

Try adding any type to your data on .subscribe like so:

load() {
    if(this.data) {
        return Promise.resolve(this.data);
    }

    return new Promise(resolve => {
        this.http.get('assets/data/questions.json').subscribe((data: any) => {
            this.data = data.questions;
            resolve(this.data);
        });
    });
}

1 Comment

Fixed the problem using the answer
0

Probably you are missing the key point which is .map(res => res.json()) and then .subscripe the data.

this.http.get('assets/data/questions.json').map(res => res.json()).subscribe(data => {
       this.data = data.questions;
       resolve(this.data);
});

I think, Since the response contains json object and which has questions: [] array, but this piece of code tries to access one level before this.data = data.questions; which is not mapped yet.

Hope this helps.,

1 Comment

Nope in HttpClientModule response object is a JSON by default, so there's no need to parse it anymore.

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.