0

I am triying yo save data using angular service like this :

   @Injectable()
    export class DataServiceProvider {


    url;
  jsonArr = [];


  constructor(public http: Http) {   
    this.url='assets/data/userProfiles.json';
  }

  getProfiles(){
    return new Promise(resolve => {
      this.http.get(this.url).
      subscribe(data => {
        resolve(data.json());
      }, err => {
        console.log(err);
      });
    });
  }


    proccessProfiles(){
        this.getProfiles().then(
          data=>{
            data[0].companies.forEach(function(cur){
              this.jsonArr.push({
                name: cur.name,
                profile: "company"
              });
            })

            data[1].centers.forEach(function(cur){
              this.jsonArr.push({
                name: cur.name,
                profile: "center"

              });
            })
          }
        )
      }

    }

when i called in my app.component.ts:

I've imported this :

import {DataServiceProvider} from '../providers/data-service/data-service';

and in this function I call:

openPopup(){
    console.log('Openpopup ');
    this.modalCtrl.create(ProfilePage).present();   


    this._ds.proccessProfiles();

  }

It return error :

Cannot read property jsonArr of undefined

Do you someone how can i solve that ?

Thank you

2 Answers 2

1

You are not instantiating that service anywhere. If you want a singleton, you need to add it in the constructor() as one of the parameters, and it gets injected there and retrieves the instance. You can then use this to access it.

constuctor(_ds: DataServiceProvider)

Sign up to request clarification or add additional context in comments.

2 Comments

I've declared it where i am using, i will put in context : User has a profiles associated it and i need to create a "global" variable where i put profiles and user in a JSON to manage that and create a logic where user can choose 1 to show in Menu like Hello "Alex" so if user click in "Admin" i will open modal with profiles like "Admin","Comunity Manager" and if user click in "Admin" now menu will Show Hello "Admin" and in modal will be "Alex" and "Comunity Manager". I hope you can help me , thank you in advance
oh, my bad. in the function if forEach, there is a different this object. You'll have to switch those to arrow functions: data[0].companies.forEach((cur) => { this.jsonArr.push({ name: cur.name, profile: "company" }); })
0

Try by binding this

proccessProfiles(){
    this.getProfiles().then(
      data=>{
        data[0].companies.forEach(function(cur){
          this.jsonArr.push({
            name: cur.name,
            profile: "company"
          });
        }.bind(this)) // changed here

        data[1].centers.forEach(function(cur){

          this.jsonArr.push({
            name: cur.name,
            profile: "center"

          });
        }.bind(this)) //changed here
      }
    )
  }

1 Comment

thank you so much but it doesnt function, if you want help me , read the top.

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.