javascript 评估预请求脚本时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48429643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:01:20  来源:igfitidea点击:

There was an error in evaluating the Pre-request Script

javascriptjsonpostman

提问by cyber8200

I'm trying to access the response of my POST request in Postman via Post Request Script.

我正在尝试通过 Post Request Script 在 Postman 中访问我的 POST 请求的响应。

I added this 2 lines, under Pre-request Script

我在下面添加了这 2 行 Pre-request Script

let response = pm.response.json(); 
console.log('JSON Response: ',response );

Then, I opened up my Postman console, before hit Sendto make my POST request

然后,我打开了我的 Postman 控制台,然后点击Send发出我的 POST 请求

I kept getting

我一直得到

There was an error in evaluating the Pre-request Script: TypeError: Cannot read property 'json' of undefined

评估预请求脚本时出错:TypeError:无法读取未定义的属性“json”

Do I need to enable anything on Postman?

我需要在 Postman 上启用任何东西吗?

回答by Clint

Pre-request scripts are ran before the request is sent. You do not have a response yet.

在发送请求之前运行预请求脚本。您还没有回复。

Try putting your script under the Teststab, which is ran after a response is received.

尝试将您的脚本放在Tests选项卡下,该选项卡在收到响应后运行。

回答by mattylantz

In my case, there was a script that was screwing up my request. If you get the postman collection from someone else, check this and try to fix it. (in my case I don't need it so I deleted it)

就我而言,有一个脚本搞砸了我的请求。如果您从其他人那里获得邮递员集合,请检查此并尝试修复它。(就我而言,我不需要它,所以我删除了它)

enter image description here

在此处输入图片说明