1

i have response from backendthat i am trying to map it to interface but its always throws interface properties undefined like in below case Envelop always coming undefined. Any idea what is implemented wrong here ?

interface.ts

export interface IResult {
    Envelop: Envelope;
}

export interface Envelope {
    $: $;
    Header: string;
    Body: Body;
}
export interface $ {
    "xmlns:soapenv": string;
}

export interface Body {
    "trk:TrackResponse": TrackShipment;
}

main.ts

public after(data: IResult){

        const result = data.Envelop.Body
        const response: any = result;
        return response;
    }

Json data from backend

"soapenv:Envelope": {
     "$": {
         "xmlns:soapenv": "http"
     },
     "soapenv:Header": "",
     "soapenv:Body": {
         "some test Data"
     }
 }
5
  • In your 'after' function, log out the data object. What keys exist on that object? It seems like the property Envelop doesn't exist on the data object. I would think this line: const result = data.Envelop.Body should look more like: const result = data['soapenv:Envelope']; Commented Aug 2, 2018 at 16:28
  • Your Envelop, Header and Body interface properties don't match the keys in the returned JSON. Commented Aug 2, 2018 at 16:31
  • Also, just FYI "soapenv:Body": { "some test Data" } is not valid json. It should be a key, value pair. Commented Aug 2, 2018 at 16:31
  • thats direct mapping to the data and i am getting the response by doing const result = data['soapenv:Envelope']; but i want to map it to interface and get it from there Commented Aug 2, 2018 at 16:32
  • "some test data" was just example i am getting undefined at first level Envelop can someone provide correct answer please Commented Aug 2, 2018 at 16:34

1 Answer 1

1

Given your response JSON, your interfaces should look like this

export interface IResult {
    "soapenv:Envelope": Envelope;
}
export interface Envelope {
    $: $;
    "soapenv:Header": string;
    "soapenv:Body": Body;
}
export interface $ {
    "xmlns:soapenv": string;
}
export interface Body {
    "trk:TrackResponse": TrackShipment;
}

Which you would then access like

public after(data: IResult) {
  const result: Body = data["soapenv:Envelope"]["soapenv:Body"];
  return result;
}
Sign up to request clarification or add additional context in comments.

8 Comments

How de we access that in the main.ts ?
can not find name Body , where its coming from ?
Body is the Body interface. If you don't have access to it in main.ts because it's in a different file, import it at the top. import { IResult, Body } from 'path/to/file';
i have imported the interface not sure its always throwing undefined on Body or any property i tried to map
What does your map look like?
|

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.