0

I'm making a call to an ODATA API, and the results of the JSON are changing after the promise is resolved.

Code block:

return client
.get(apiQueryString, AadHttpClient.configurations.v1)
.then((response: HttpClientResponse) => {
    if (response.ok) {
        return response.json();
    }
    else {
        console.warn(response);
        return;
    }
})
.then((json: { value: INewBusinessProps[] }) =>{
    return json.value;
})

If I throw in a console.log(response.json()); after the if (response.ok) line, I get the data back correctly:

{first_name: "Fake", last_name: "User", user_type: "billing"}
{first_name: "Fake", last_name: "User", user_type: "originating"}
{first_name: "Fake", last_name: "User", user_type: "responsible"}

However, when I add console.log(json.value) after the line .then((json: { value: INewBusinessProps[] }) =>{, I get:

{first_name: "Fake", last_name: "User", user_type: "originating"}
{first_name: "Fake", last_name: "User", user_type: "originating"}
{first_name: "Fake", last_name: "User", user_type: "originating"}

(note the user_type)

Running from a browser or fiddler gives me the correct results; it's only after the then that the data gets messed up.

I also thought it might be the type INewBusinessProps[] that was the problem, so I changed it to any[] and it still did not work.

Randy Slavey
  • 544
  • 4
  • 19

1 Answers1

0

Found it. It was a syntax error. Later in the code, I was filtering by user_type and had

return person
.filter(p => p.user_type = userType)

which was setting the user_type instead of comparing it. Changed it to:

return person
.filter(p => p.user_type == userType)

and it works.

Randy Slavey
  • 544
  • 4
  • 19