Javascript 如何创建可以在邮递员请求中多次使用的生成器函数?
我想创建一个在请求主体内部使用的函数 每次调用函数时,它都返回不同的值;每次调用时增加1的数字 我想在postman请求中多次调用此函数,以便在整个请求正文中插入一系列数字 e、 gJavascript 如何创建可以在邮递员请求中多次使用的生成器函数?,javascript,rest,templates,request,postman,Javascript,Rest,Templates,Request,Postman,我想创建一个在请求主体内部使用的函数 每次调用函数时,它都返回不同的值;每次调用时增加1的数字 我想在postman请求中多次调用此函数,以便在整个请求正文中插入一系列数字 e、 g 有什么方法可以做到这一点吗?您可以通过内联调用以声明式的方式将对象构建为javascript let response = {body: { "first": {nextSequenceNumber()}, "second": {nextSequenceNumber()}, "third": {nextSequen
有什么方法可以做到这一点吗?您可以通过内联调用以声明式的方式将对象构建为javascript
let response = {body: {
"first": {nextSequenceNumber()},
"second": {nextSequenceNumber()},
"third": {nextSequenceNumber()},
"fourth": {nextSequenceNumber()}}};
response.body.someField = ‘foo’(
在返回块中(使用express.js等),发送您构建的js对象 您可以通过内联调用以声明式样式将对象构建为javascript
let response = {body: {
"first": {nextSequenceNumber()},
"second": {nextSequenceNumber()},
"third": {nextSequenceNumber()},
"fourth": {nextSequenceNumber()}}};
response.body.someField = ‘foo’(
在返回块中(使用express.js等),发送您构建的js对象 您可以获取一个函数并获取下一次调用的value属性
函数*getNext(i){
而(真)产生i++;
}
var sequenceNumber=getNext(0),
对象={
第一个:sequenceNumber.next().value,
第二个:sequenceNumber.next().value,
第三个:sequenceNumber.next().value,
第四:sequenceNumber.next().value
};
console.log(对象)
您可以获取一个函数并获取下一次调用的value属性
函数*getNext(i){
而(真)产生i++;
}
var sequenceNumber=getNext(0),
对象={
第一个:sequenceNumber.next().value,
第二个:sequenceNumber.next().value,
第三个:sequenceNumber.next().value,
第四:sequenceNumber.next().value
};
console.log(对象)代码>这是一种可怕的黑客方式,但它会给您一个如下所示的对象:
{
"first":1,
"second":2,
"third":3,
"forth":4
}
如果您将此添加到请求的请求前脚本
:
var n = 0;
function increment() { return ++n; }
function requestBody(first, second, third, forth) {
this.first = first
this.second = second
this.third = third
this.forth = forth
}
let myData = new requestBody(increment(), increment(), increment(), increment())
pm.globals.set('bodyData', JSON.stringify(myData))
您可以使用{{bodyData}}
变量作为请求的主体:
同样,这不是一个很好的解决方案,但它可能是一个值得反复研究的问题 这是一种可怕的黑客方式,但它会给你一个如下所示的对象:
{
"first":1,
"second":2,
"third":3,
"forth":4
}
如果您将此添加到请求的请求前脚本
:
var n = 0;
function increment() { return ++n; }
function requestBody(first, second, third, forth) {
this.first = first
this.second = second
this.third = third
this.forth = forth
}
let myData = new requestBody(increment(), increment(), increment(), increment())
pm.globals.set('bodyData', JSON.stringify(myData))
您可以使用{{bodyData}}
变量作为请求的主体:
同样,这不是一个很好的解决方案,但它可能是一个值得反复研究的问题 它不应该像你写的那样“正常工作”吗?是什么导致了这个问题?它不应该像你写的那样“正常工作”吗?是什么导致了这个问题?