question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[critical] POST requests with JSON data using a {{variable}} set to undefined or null fails with error: Cannot read property 'toString' of undefined

See original GitHub issue

Newman exists when a variable is being used in the request body, but the variable was set to undefined or null, as it was not available in the previous request where it was being set.

Rather than passing the variable as it is and the tests failing, newman exits with error and no reports are created.

Error shown by newman: Cannot read property β€˜toString’ of undefined

  1. Newman Version (can be found via newman -v): 3.9.2
  2. OS details (type, version, and architecture): Windows 7 Pro, x64
  3. Are you using Newman as a library, or via the CLI? CLI
  4. Did you encounter this recently, or has this bug always been there: Probably after 3.8.2
  5. Expected behaviour: Any undefined or null variables should be resolved to undefined/null and sent accross
  6. Command / script used to run Newman:
  7. Sample collection, and auxiliary files (minus the sensitive details): Pasted at the end
  8. Screenshots (if applicable):

Steps to reproduce the problem:

  1. Create a POST Request with JSON body
  2. In Pre-requisite or a request before, Set the variable to undefined. For example:
pm.environment.set("version", undefined);
  1. In JSON body use a variable, for example :
{"version":"{{version}}"}
  1. Run the collection using newman

Newman CLI shows following output: Temp

β†’ After comparisons GET https://postman-echo.com/time/after?timestamp={{start}}&target=2017-10-10 [400 Bad Request, 408B, 1622ms]

β†’ POST Raw Text

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ executed β”‚ failed β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ iterations β”‚ 0 β”‚ 0 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ requests β”‚ 1 β”‚ 0 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ test-scripts β”‚ 1 β”‚ 0 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ prerequest-scripts β”‚ 1 β”‚ 0 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ assertions β”‚ 0 β”‚ 0 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ total run duration: 0ms β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ total data received: 0B (approx) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ average response time: 1622ms β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Cannot read property β€˜toString’ of undefined

Postman Collection Runner gives following error stack: message : β€œCannot read property β€˜toString’ of undefined” name : β€œTypeError” stack : β€œTypeError: Cannot read property β€˜toString’ of undefined↡ at PostmanVariable.toString (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\variable.js:108:34)↡ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:221:53↡ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:47:23↡ at RegExp.[Symbol.replace] (<anonymous>)↡ at String.replace (native)↡ at SuperString.replace (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:45:34)↡ at Substitutor.parse (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\superstring\index.js:132:27)↡ at Function.replaceSubstitutions (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:222:69)↡ at Function.<anonymous> (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:253:25)↡ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:247:40↡ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:4944:15↡ at baseForOwn (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:3001:24)↡ at Function.forOwn (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:13002:24)↡ at Function.<anonymous> (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:246:19)↡ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\lib\collection\property.js:247:40↡ at C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:4944:15↡ at baseForOwn (C:\Users\aadm221\AppData\Local\Postman\app-5.5.0\resources\app.asar\node_modules\postman-collection\node_modules\lodash\lodash.js:3001:24)”

Sample Collection:

{
	"info": {
		"name": "Temp",
		"_postman_id": "734b99af-72b1-a510-dc4c-10214c91aeee",
		"description": "",
		"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
	},
	"item": [
		{
			"name": "After comparisons",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "b8d9c216-89af-4c77-8ff6-7066d5e8fd77",
						"type": "text/javascript",
						"exec": [
							"pm.environment.set(\"version\", undefined);"
						]
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"body": {},
				"url": {
					"raw": "https://postman-echo.com/time/after?timestamp={{start}}&target=2017-10-10",
					"protocol": "https",
					"host": [
						"postman-echo",
						"com"
					],
					"path": [
						"time",
						"after"
					],
					"query": [
						{
							"key": "timestamp",
							"value": "{{start}}",
							"equals": true
						},
						{
							"key": "target",
							"value": "2017-10-10",
							"equals": true
						}
					]
				},
				"description": "A simple `GET` request to `/time/after` to check if the provided timestamps is after a comparison `target` (query parameter).\n\nThis endpoint accepts `timestamp`, `locale`, `format`, `strict`, and `target` query parameters to construct the date time instance to check against.\n\nResponses are provided in JSON format, with a `after` key to indicate the result. The response code is `200` for valid query parameters, and `400` otherwise.\n\n```\n{\n  after: true/false\n}\n```"
			},
			"response": []
		},
		{
			"name": "POST Raw Text",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "eca79d5c-0416-4fc6-b4b6-eafe6f371f05",
						"type": "text/javascript",
						"exec": [
							"var responseJSON;",
							"",
							"try { ",
							"    responseJSON = JSON.parse(responseBody); ",
							"    tests['response is valid JSON'] = true;",
							"}",
							"catch (e) { ",
							"    responseJSON = {}; ",
							"    tests['response is valid JSON'] = false;",
							"}",
							"",
							"",
							"tests['response has post data'] = _.has(responseJSON, 'data');",
							"tests['response matches the data posted'] = (responseJSON.data && responseJSON.data.length === 256);",
							"",
							"tests[\"content-type equals text/plain\"] = responseJSON && responseJSON.headers && (responseJSON.headers[\"content-type\"] === 'text/plain');"
						]
					}
				},
				{
					"listen": "prerequest",
					"script": {
						"id": "c5d968db-e436-46e3-b4fe-cda9d6ebab89",
						"type": "text/javascript",
						"exec": [
							""
						]
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "Content-Type",
						"value": "application/json"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\n\t\"name\":\"postman\",\n\t\"value\":\"{{version}}\"\n}"
				},
				"url": "https://postman-echo.com/post",
				"description": "The HTTP `POST` request method is meant to transfer data to a server \n(and elicit a response). What data is returned depends on the implementation\nof the server.\n\nA `POST` request can pass parameters to the server using \"Query String \nParameters\", as well as the Request Body. For example, in the following request,\n\n> POST /hi/there?hand=wave\n>\n> <request-body>\n\nThe parameter \"hand\" has the value \"wave\". The request body can be in multiple\nformats. These formats are defined by the MIME type of the request. The MIME \nType can be set using the ``Content-Type`` HTTP header. The most commonly used \nMIME types are:\n\n* `multipart/form-data`\n* `application/x-www-form-urlencoded`\n* `application/json`\n\nThis endpoint echoes the HTTP headers, request parameters, the contents of\nthe request body and the complete URI requested."
			},
			"response": []
		}
	]
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aksbenzcommented, Jan 14, 2018

Not intentionally sending undefined.

There are a lot of cases, where some data is set from the response of a previous request , but in case no data is received for that field, it gets set to undefined/null. Don’t want to put all kinds of null checks in these requests as they are used for fetching data.

So the later request which is using this variable would eventually fail, since the response would give error.

But I do not expect newman to completely exit with no reports and without finishing with all the tests.

Also, I am sure this was working in 3.8.2

0reactions
kunagpalcommented, Jan 19, 2018

@aksbenz This has been fixed with Newman v3.9.3. πŸ˜„

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'toString' of Undefined in JavaScript
The "Cannot read property 'toString' of undefined" error occurs when the toString() method is called on an undefined value. To solve the error,Β ......
Read more >
node.js - Express Post method can't read property 'toString'
You seem to be getting the body as a string, not a json. First, make sure you have added the json middleware to...
Read more >
useswr cannot read properties of undefined - You.com
I'm trying to fetch data via UseSWR, which works fine. But when i call it again on click in my app, this error...
Read more >
Component Reference - Apache JMeter - User's Manual
Several test elements use JMeter properties to control their behaviour. These properties are normally resolved when the class is loaded.
Read more >
Karate | Test Automation Made Simple.
The BDD syntax popularized by Cucumber is language-neutral, and easy for even ... Re-use of payload-data and user-defined functions across tests is so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found