1

I'm farly new with Typescript and struggle with what seems to be a dumb issue:

I retrieve an 'agent' object from a service.

this.agentsController.getAgent(matricule).subscribe({
      next: agent => {
        console.log(agent)
        console.log(agent.emailPro)
      },
      error: (error: string) => {
        console.log(error)
      }
    });

According to the 1st console.log, it is well populated :

{
    "agent": {
        "matricule": "000001",
        "nom": "DummyName",
        "prenom": "DummyFirstname",
        "emailPro": "dummy@dummy.fr",
        "adresse1": "dummy address",
        "telephonePerso": "0000000001"
    }
}

But as clearly as the print of the agent shows a well defined email address (emailPro), the 2nd console.log always shows undefined

How is that possible ? What am I missing ?

Thomas L.
  • 11
  • 3
  • 2
    I believe this is not the case @T.J.Crowder The error seems to be that the correct field is `agent.agent.emailPro`. – pietro909 Jul 22 '21 at 09:54
  • 1
    @pietro909 - **Great eyes!** That's a really, really good catch. There's bound to be a duplicate for that as well, although it nearly falls in the "typo" category... – T.J. Crowder Jul 22 '21 at 09:57
  • 1
    > Great eyes! having had LASIK recently, you words are very welcome <3 thanks! – pietro909 Jul 22 '21 at 10:00
  • 2
    I'm quite impressed by your swiftness ! That works perfectly fine now. At the end, I fixed my service so it returns the actual agent instead of some 'agent' called object containing itslef my proper agent. That's what led me to this confusion. Thanks again – Thomas L. Jul 22 '21 at 10:01
  • @pietro909 - Oooh, are you happy with the results? I've been considering it.... – T.J. Crowder Jul 22 '21 at 10:05
  • @T.J.Crowder : It is as much a typo than it is a wrong parsing of my service response. If it can help on both situation for newbies like me ;) – Thomas L. Jul 22 '21 at 10:06
  • @thlac - If you think the way you defined your service is a mistake others are going to make, and you can't find that situation described elsewhere on SO, you could update your question to show the problematic service definition, then post an answer (it's perfectly okay to answer your own question) showing the error and how to fix it. :-) – T.J. Crowder Jul 22 '21 at 10:11
  • @T.J.Crowder : Done, thanks for the tip – Thomas L. Jul 22 '21 at 10:14
  • 1
    @T.J.Crowder very happy tbh :-) I was really scared but decided to give it a go. Be ready to loose some night vision and feel sometimes uncomfortable in the morning (dry eyes) though. I'm fine with both issues and I'd say my life's quality improved. – pietro909 Jul 22 '21 at 10:16
  • @pietro909 - Thanks! Personal question: Roughly how old are you? I'm mid-fifties. :-) (Things are different for 30somethings than 50somethings. ;-) ) – T.J. Crowder Jul 22 '21 at 10:18
  • 1
    37 and reportedly healthy :D I suggest you look for professional medical advice, but consider me available for subjective questions related to how it feels before/after. Good luck! - edit: I had inherited myopia ~4.0 and got 20/20 at the last check (8 months after). – pietro909 Jul 22 '21 at 10:25

1 Answers1

0

To summaries the comments chain as a valid response :

My service was encapsulating my proper agent object in some other dummy object that happens to be called 'agent' as well. Hence the confusion !

So instead of calling agent.EmailPro I should have called agent.agent.EmailPro.

I chose to fix my service instead so it parsed the API response better :

From this.httpClient.get('apiUrl').pipe(map((result:any) => result as Agent));

To this.httpClient.get('apiUrl').pipe(map((result:any) => result.agent as Agent));

Thanks again to @pietro909 & @T.J.Crowder

Thomas L.
  • 11
  • 3