Angular - service object populated returns null












0















I am using Angular 5 and ionic 3, what I am trying to do is to populate an object following a response endpoint, like this:



user.service.ts



public UserInfo: UserData = {
id: null,
username: null,
email: null,
subscribeData: null,
brand: null,
fiscalcode: null,
};

populateBaseUserInfo(user) {
this.UserInfo.id = user._id;
this.UserInfo.username = user.username;
this.UserInfo.subscribeData = user.subscribeData;
this.UserInfo.email = user.email;
console.log(JSON.stringify(this.UserInfo));
}


The response in console.log is ok:



the response in console.log is ok



but when I try to take the object in the component, the object returns all the fields as null...



user.component.ts



import { UserService } from '../../service/user.service';
@Component({
providers: [UserService]
})
export class MainPage {
public localUser: UserData;

constructor(private UserService : UserService, ) {
this.localUser = this.UserService.UserInfo;
}
)


user.html



{{localUser | json}}


enter image description here



So, what am I doing wrong? What needs to be done to see the object json in the component as the same from the response?



Thank you










share|improve this question

























  • What is the content of UtentiService service? Does it call populateBaseUserInfo(user) method of UserService?

    – Harun Yılmaz
    Dec 29 '18 at 9:04











  • your service is working fine. even it's showing you data also correct. U are printing default object which is null in your service.

    – Ashish Ratan
    Dec 29 '18 at 9:04











  • @HarunYılmaz sorry a wrong reference (written in italian)! i have updated

    – Pds Ink
    Dec 29 '18 at 9:08











  • @AshishRatan ok, understand, but if i want to override?

    – Pds Ink
    Dec 29 '18 at 9:08











  • That's because you declare a provider for the service in the component: that tells Angular to create a new instance of the service for each nstance of the component. Declare the provider in the module, to make the service a singleton.

    – JB Nizet
    Dec 29 '18 at 9:13
















0















I am using Angular 5 and ionic 3, what I am trying to do is to populate an object following a response endpoint, like this:



user.service.ts



public UserInfo: UserData = {
id: null,
username: null,
email: null,
subscribeData: null,
brand: null,
fiscalcode: null,
};

populateBaseUserInfo(user) {
this.UserInfo.id = user._id;
this.UserInfo.username = user.username;
this.UserInfo.subscribeData = user.subscribeData;
this.UserInfo.email = user.email;
console.log(JSON.stringify(this.UserInfo));
}


The response in console.log is ok:



the response in console.log is ok



but when I try to take the object in the component, the object returns all the fields as null...



user.component.ts



import { UserService } from '../../service/user.service';
@Component({
providers: [UserService]
})
export class MainPage {
public localUser: UserData;

constructor(private UserService : UserService, ) {
this.localUser = this.UserService.UserInfo;
}
)


user.html



{{localUser | json}}


enter image description here



So, what am I doing wrong? What needs to be done to see the object json in the component as the same from the response?



Thank you










share|improve this question

























  • What is the content of UtentiService service? Does it call populateBaseUserInfo(user) method of UserService?

    – Harun Yılmaz
    Dec 29 '18 at 9:04











  • your service is working fine. even it's showing you data also correct. U are printing default object which is null in your service.

    – Ashish Ratan
    Dec 29 '18 at 9:04











  • @HarunYılmaz sorry a wrong reference (written in italian)! i have updated

    – Pds Ink
    Dec 29 '18 at 9:08











  • @AshishRatan ok, understand, but if i want to override?

    – Pds Ink
    Dec 29 '18 at 9:08











  • That's because you declare a provider for the service in the component: that tells Angular to create a new instance of the service for each nstance of the component. Declare the provider in the module, to make the service a singleton.

    – JB Nizet
    Dec 29 '18 at 9:13














0












0








0








I am using Angular 5 and ionic 3, what I am trying to do is to populate an object following a response endpoint, like this:



user.service.ts



public UserInfo: UserData = {
id: null,
username: null,
email: null,
subscribeData: null,
brand: null,
fiscalcode: null,
};

populateBaseUserInfo(user) {
this.UserInfo.id = user._id;
this.UserInfo.username = user.username;
this.UserInfo.subscribeData = user.subscribeData;
this.UserInfo.email = user.email;
console.log(JSON.stringify(this.UserInfo));
}


The response in console.log is ok:



the response in console.log is ok



but when I try to take the object in the component, the object returns all the fields as null...



user.component.ts



import { UserService } from '../../service/user.service';
@Component({
providers: [UserService]
})
export class MainPage {
public localUser: UserData;

constructor(private UserService : UserService, ) {
this.localUser = this.UserService.UserInfo;
}
)


user.html



{{localUser | json}}


enter image description here



So, what am I doing wrong? What needs to be done to see the object json in the component as the same from the response?



Thank you










share|improve this question
















I am using Angular 5 and ionic 3, what I am trying to do is to populate an object following a response endpoint, like this:



user.service.ts



public UserInfo: UserData = {
id: null,
username: null,
email: null,
subscribeData: null,
brand: null,
fiscalcode: null,
};

populateBaseUserInfo(user) {
this.UserInfo.id = user._id;
this.UserInfo.username = user.username;
this.UserInfo.subscribeData = user.subscribeData;
this.UserInfo.email = user.email;
console.log(JSON.stringify(this.UserInfo));
}


The response in console.log is ok:



the response in console.log is ok



but when I try to take the object in the component, the object returns all the fields as null...



user.component.ts



import { UserService } from '../../service/user.service';
@Component({
providers: [UserService]
})
export class MainPage {
public localUser: UserData;

constructor(private UserService : UserService, ) {
this.localUser = this.UserService.UserInfo;
}
)


user.html



{{localUser | json}}


enter image description here



So, what am I doing wrong? What needs to be done to see the object json in the component as the same from the response?



Thank you







angular typescript service ionic3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 10:28









marc_s

573k12811071255




573k12811071255










asked Dec 29 '18 at 8:59









Pds InkPds Ink

265421




265421













  • What is the content of UtentiService service? Does it call populateBaseUserInfo(user) method of UserService?

    – Harun Yılmaz
    Dec 29 '18 at 9:04











  • your service is working fine. even it's showing you data also correct. U are printing default object which is null in your service.

    – Ashish Ratan
    Dec 29 '18 at 9:04











  • @HarunYılmaz sorry a wrong reference (written in italian)! i have updated

    – Pds Ink
    Dec 29 '18 at 9:08











  • @AshishRatan ok, understand, but if i want to override?

    – Pds Ink
    Dec 29 '18 at 9:08











  • That's because you declare a provider for the service in the component: that tells Angular to create a new instance of the service for each nstance of the component. Declare the provider in the module, to make the service a singleton.

    – JB Nizet
    Dec 29 '18 at 9:13



















  • What is the content of UtentiService service? Does it call populateBaseUserInfo(user) method of UserService?

    – Harun Yılmaz
    Dec 29 '18 at 9:04











  • your service is working fine. even it's showing you data also correct. U are printing default object which is null in your service.

    – Ashish Ratan
    Dec 29 '18 at 9:04











  • @HarunYılmaz sorry a wrong reference (written in italian)! i have updated

    – Pds Ink
    Dec 29 '18 at 9:08











  • @AshishRatan ok, understand, but if i want to override?

    – Pds Ink
    Dec 29 '18 at 9:08











  • That's because you declare a provider for the service in the component: that tells Angular to create a new instance of the service for each nstance of the component. Declare the provider in the module, to make the service a singleton.

    – JB Nizet
    Dec 29 '18 at 9:13

















What is the content of UtentiService service? Does it call populateBaseUserInfo(user) method of UserService?

– Harun Yılmaz
Dec 29 '18 at 9:04





What is the content of UtentiService service? Does it call populateBaseUserInfo(user) method of UserService?

– Harun Yılmaz
Dec 29 '18 at 9:04













your service is working fine. even it's showing you data also correct. U are printing default object which is null in your service.

– Ashish Ratan
Dec 29 '18 at 9:04





your service is working fine. even it's showing you data also correct. U are printing default object which is null in your service.

– Ashish Ratan
Dec 29 '18 at 9:04













@HarunYılmaz sorry a wrong reference (written in italian)! i have updated

– Pds Ink
Dec 29 '18 at 9:08





@HarunYılmaz sorry a wrong reference (written in italian)! i have updated

– Pds Ink
Dec 29 '18 at 9:08













@AshishRatan ok, understand, but if i want to override?

– Pds Ink
Dec 29 '18 at 9:08





@AshishRatan ok, understand, but if i want to override?

– Pds Ink
Dec 29 '18 at 9:08













That's because you declare a provider for the service in the component: that tells Angular to create a new instance of the service for each nstance of the component. Declare the provider in the module, to make the service a singleton.

– JB Nizet
Dec 29 '18 at 9:13





That's because you declare a provider for the service in the component: that tells Angular to create a new instance of the service for each nstance of the component. Declare the provider in the module, to make the service a singleton.

– JB Nizet
Dec 29 '18 at 9:13












2 Answers
2






active

oldest

votes


















0














I think the problem there is that since you are just changing properties in your object you are not triggering the change detection, I'm sure that everything would work if instead of returning the reference your service method return a copy of that object ( for example make UserInfo a getter that return lodash clone of that object ) notice that is not the right solution even if it's solve your problem. Would be better if every time you call populateBasicUserInfo you just create a new object with the desired info instead of just replacing the property . In this way as I was saying since you change the reference it will run the detection.






share|improve this answer































    0














    I solved, the main problem was that "userInfo" was not recognized as a singleton, due to version of angular (5), i can't use the providein:root parameter in the injectable of the service, so this is what i have done:



    -move provider of the service inside the app.module and remove from component



    -use Behaviorsubject rxjs as the follow inside service:



    import { BehaviorSubject } from 'rxjs/BehaviorSubject';
    public UserInfo = new BehaviorSubject<any>({
    _id : null,
    username : null,
    email : null,
    data : null,
    brand : null,
    fiscalcode : null
    });
    user = this.UserInfo.asObservable();

    populateBaseUserInfo(user : any){
    this.UserInfo.next(user)
    console.log(JSON.stringify(this.UserInfo));
    };


    here the code component where i call "populateBaseUserinfo"



    this.UtentiService.populateBaseUserInfo( this.risposta );


    and here another component where i call the information updated within observable:



       this.UtentiService.user.subscribe(user =>{
    this.localUser = user;
    })





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968147%2fangular-service-object-populated-returns-null%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I think the problem there is that since you are just changing properties in your object you are not triggering the change detection, I'm sure that everything would work if instead of returning the reference your service method return a copy of that object ( for example make UserInfo a getter that return lodash clone of that object ) notice that is not the right solution even if it's solve your problem. Would be better if every time you call populateBasicUserInfo you just create a new object with the desired info instead of just replacing the property . In this way as I was saying since you change the reference it will run the detection.






      share|improve this answer




























        0














        I think the problem there is that since you are just changing properties in your object you are not triggering the change detection, I'm sure that everything would work if instead of returning the reference your service method return a copy of that object ( for example make UserInfo a getter that return lodash clone of that object ) notice that is not the right solution even if it's solve your problem. Would be better if every time you call populateBasicUserInfo you just create a new object with the desired info instead of just replacing the property . In this way as I was saying since you change the reference it will run the detection.






        share|improve this answer


























          0












          0








          0







          I think the problem there is that since you are just changing properties in your object you are not triggering the change detection, I'm sure that everything would work if instead of returning the reference your service method return a copy of that object ( for example make UserInfo a getter that return lodash clone of that object ) notice that is not the right solution even if it's solve your problem. Would be better if every time you call populateBasicUserInfo you just create a new object with the desired info instead of just replacing the property . In this way as I was saying since you change the reference it will run the detection.






          share|improve this answer













          I think the problem there is that since you are just changing properties in your object you are not triggering the change detection, I'm sure that everything would work if instead of returning the reference your service method return a copy of that object ( for example make UserInfo a getter that return lodash clone of that object ) notice that is not the right solution even if it's solve your problem. Would be better if every time you call populateBasicUserInfo you just create a new object with the desired info instead of just replacing the property . In this way as I was saying since you change the reference it will run the detection.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 29 '18 at 10:45









          Vale SteveVale Steve

          555312




          555312

























              0














              I solved, the main problem was that "userInfo" was not recognized as a singleton, due to version of angular (5), i can't use the providein:root parameter in the injectable of the service, so this is what i have done:



              -move provider of the service inside the app.module and remove from component



              -use Behaviorsubject rxjs as the follow inside service:



              import { BehaviorSubject } from 'rxjs/BehaviorSubject';
              public UserInfo = new BehaviorSubject<any>({
              _id : null,
              username : null,
              email : null,
              data : null,
              brand : null,
              fiscalcode : null
              });
              user = this.UserInfo.asObservable();

              populateBaseUserInfo(user : any){
              this.UserInfo.next(user)
              console.log(JSON.stringify(this.UserInfo));
              };


              here the code component where i call "populateBaseUserinfo"



              this.UtentiService.populateBaseUserInfo( this.risposta );


              and here another component where i call the information updated within observable:



                 this.UtentiService.user.subscribe(user =>{
              this.localUser = user;
              })





              share|improve this answer




























                0














                I solved, the main problem was that "userInfo" was not recognized as a singleton, due to version of angular (5), i can't use the providein:root parameter in the injectable of the service, so this is what i have done:



                -move provider of the service inside the app.module and remove from component



                -use Behaviorsubject rxjs as the follow inside service:



                import { BehaviorSubject } from 'rxjs/BehaviorSubject';
                public UserInfo = new BehaviorSubject<any>({
                _id : null,
                username : null,
                email : null,
                data : null,
                brand : null,
                fiscalcode : null
                });
                user = this.UserInfo.asObservable();

                populateBaseUserInfo(user : any){
                this.UserInfo.next(user)
                console.log(JSON.stringify(this.UserInfo));
                };


                here the code component where i call "populateBaseUserinfo"



                this.UtentiService.populateBaseUserInfo( this.risposta );


                and here another component where i call the information updated within observable:



                   this.UtentiService.user.subscribe(user =>{
                this.localUser = user;
                })





                share|improve this answer


























                  0












                  0








                  0







                  I solved, the main problem was that "userInfo" was not recognized as a singleton, due to version of angular (5), i can't use the providein:root parameter in the injectable of the service, so this is what i have done:



                  -move provider of the service inside the app.module and remove from component



                  -use Behaviorsubject rxjs as the follow inside service:



                  import { BehaviorSubject } from 'rxjs/BehaviorSubject';
                  public UserInfo = new BehaviorSubject<any>({
                  _id : null,
                  username : null,
                  email : null,
                  data : null,
                  brand : null,
                  fiscalcode : null
                  });
                  user = this.UserInfo.asObservable();

                  populateBaseUserInfo(user : any){
                  this.UserInfo.next(user)
                  console.log(JSON.stringify(this.UserInfo));
                  };


                  here the code component where i call "populateBaseUserinfo"



                  this.UtentiService.populateBaseUserInfo( this.risposta );


                  and here another component where i call the information updated within observable:



                     this.UtentiService.user.subscribe(user =>{
                  this.localUser = user;
                  })





                  share|improve this answer













                  I solved, the main problem was that "userInfo" was not recognized as a singleton, due to version of angular (5), i can't use the providein:root parameter in the injectable of the service, so this is what i have done:



                  -move provider of the service inside the app.module and remove from component



                  -use Behaviorsubject rxjs as the follow inside service:



                  import { BehaviorSubject } from 'rxjs/BehaviorSubject';
                  public UserInfo = new BehaviorSubject<any>({
                  _id : null,
                  username : null,
                  email : null,
                  data : null,
                  brand : null,
                  fiscalcode : null
                  });
                  user = this.UserInfo.asObservable();

                  populateBaseUserInfo(user : any){
                  this.UserInfo.next(user)
                  console.log(JSON.stringify(this.UserInfo));
                  };


                  here the code component where i call "populateBaseUserinfo"



                  this.UtentiService.populateBaseUserInfo( this.risposta );


                  and here another component where i call the information updated within observable:



                     this.UtentiService.user.subscribe(user =>{
                  this.localUser = user;
                  })






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 17:50









                  Pds InkPds Ink

                  265421




                  265421






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968147%2fangular-service-object-populated-returns-null%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas