在 Postman 中使用 math.js

Using math.js in Postman

我正在尝试在 Postman 中使用 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);。 None 其中有效。 函数 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);
});