# TypeError when calling a endpoint with an async request

**URL:** <https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440>\
**Category:** Help Hub\
**Tags:** variables, scripts, pmsendrequest\
**Created:** [July 22, 2024, 10:47pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440 "2024-07-22T22:47:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minhaco](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/minhaco/32/29464_2.png) [@Minhaco](https://community.postman.com/u/Minhaco)\
**Post date:** [July 22, 2024, 10:47pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/1 "2024-07-22T22:47:52Z")

</div>

I’m have a script that calls an API to get a token. I saved this token in a environment variable and it was working correctly, but I don’t know why I’m getting this error:

> TypeError: Cannot read properties of undefined (reading ‘json’)

```auto
pm.sendRequest({
  url: pm.environment.get("url") + 'login',
  method: 'POST', 
  header: {
    'content-type': 'application/json'
  },
  body: {
    mode: 'raw'
    raw: JSON.stringfy({
      "username": pm.environment.get("username"),
      "password": pm.environment.get("password")
    })
}
}, function(err, res) {
  pm.environment.set("token", res.json().resultObject.token);
})

```

Anyone knows what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [July 23, 2024, 9:27am UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/2 "2024-07-23T09:27:04Z")

</div>

Hey @Minhaco 👋

Welcome to the Postman Community! :postman:

Can you open the Postman Console to check what is actually getting sent in the request and what’s returned as the response body.

Here’s an example of doing that for a sample endpoint:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/d/6d07ea50ec65b9ffa928a890d879c3bbec6fb639.png)

* * *

- What’s the value of the `url` variable? Does that have a trailing `/`?
- Is the correct environment selected?
- Are the correct credentials being sent in the request?

We’ll need to build up the contextual information here to establish what might be happening.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 23, 2024, 1:11pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/3 "2024-07-23T13:11:51Z")

</div>

Following on from Danny’s advice.

This part of your script can be extended to hande error situations.

```auto
}, function(err, res) {
  pm.environment.set("token", res.json().resultObject.token);
})

```

Consider the following.

```auto
function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.test("Send request status code = 200", () => {
            pm.expect(res).to.have.status(200);
            let resJson = res.json();
            // console.log(resJson);
            pm.environment.set("token", resJson.resultObject.token);
            // console.log(pm.environment.get("token"));
        });
    }
};

```

This includes a test and some console log placeholders for troubleshooting.

The error you are getting is because the res object is not defined. Therefore the json() element doesn’t exist causing the error.

---

<div class="post-metadata">

**Author:** ![Minhaco](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/minhaco/32/29464_2.png) [@Minhaco](https://community.postman.com/u/Minhaco)\
**Post date:** [July 23, 2024, 2:05pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/4 "2024-07-23T14:05:44Z")

</div>

- What’s the value of the `url` variable? Does that have a trailing `/`?  
R: The value has the start part of the full endpoint without the last ‘/’.
- Is the correct environment selected?  
R: Yes, I’ve checked.
- Are the correct credentials being sent in the request?  
R: Yes, because it was working before.

---

<div class="post-metadata">

**Author:** ![Minhaco](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/minhaco/32/29464_2.png) [@Minhaco](https://community.postman.com/u/Minhaco)\
**Post date:** [July 23, 2024, 2:14pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/5 "2024-07-23T14:14:31Z")

</div>

I made the update on my script following the recomendation of Mike Jones and both of you are corrects.

Probably someone altered the credentials to log in and I couldn’t catch this error with the previous code, because it was not prepared to receive a statusCode 204.

Thank you so much!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [July 23, 2024, 2:14pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/6 "2024-07-23T14:14:43Z")

</div>

Just looking at part your initial script again:

```auto
pm.sendRequest({
  url: pm.environment.get("url") + 'login',
  method: 'POST', 
  header: {
    'content-type': 'application/json'
  },
  body: {
    mode: 'raw'
    raw: JSON.stringfy({
      "username": pm.environment.get("username"),
      "password": pm.environment.get("password")
    })
}

```

That would have been throwing an error in 2 places with the _stringfy_ and the missing comma after _raw_

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [July 23, 2024, 2:22pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/7 "2024-07-23T14:22:31Z")

</div>

There’s no reason why that wouldn’t have worked, unless it wasn’t successfully completing the request and returning JSON, like it was returning a HTML error page etc.

```auto
pm.environment.set("token", res.json().resultObject.token);

```

It would be the same as doing this on the main request:

```auto
pm.environment.set("token", pm.response.json().something.data);

```

Although assigning in to a variable first is a better practice.

* * *

Adding the test in there is a great practice to be sure that the request is actually doing what’s expected, with the data that’s being sent.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [July 26, 2024, 2:23pm UTC](https://community.postman.com/t/typeerror-when-calling-a-endpoint-with-an-async-request/64440/8 "2024-07-26T14:23:19Z")

</div>

Closed on July 26, 2024, 2:23pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
