问题描述
我正在尝试在邮递员中使用math.js
。
已经看到Tip#5 in their website。所以,在一个请求中我有
postman.setGlobalVariable("mathjs",() => {
\\ The full math.js library
});
具体来说,this is the code of math.js
that I copied,以防版本问题。
然后在应该使用库的请求中,我评估全局变量
eval(globals.mathjs)();
我不经常使用JavaScript,所以也许这是我缺少的基本知识。在第一个请求中,定义了一个全局变量mahjs
,该值是一个调用库代码的lambda。然后,在第二个请求中,该lambda函数被调用。如果我到目前为止的理解不正确,请纠正我。
我尝试过:math.multiply(x,y);
,Math.multiply(x,y);
,multiply(x,y);
。它们都不是有效的。
函数multiply
seems to be defined by the library用作math.multiply(array,matrix)
。
与我已经完成的重用进行比较。
在一个请求中
postman.setGlobalVariable("utils",() => {
myfunction = function (x){
return x+1;
};
});
以及在使用它的请求中
eval(globals.utils)();
x = 1;
console.log(myfunction(x));
这有效。
解决方法
这可以解决您的问题:
const mathjsUrl = "https://cdnjs.cloudflare.com/ajax/libs/mathjs/7.5.1/math.min.js";
pm.sendRequest(mathjsUrl,(err,response) => {
const mathjs = response.text();
(new Function(mathjs))();
let result = math.multiply(4,3);;
console.log(result);
});