{ \\ Th" /> { \\ Th" /> { \\ Th"/>

Using math.js in Postman

947 Views Asked by At

I am trying to use math.js in Postman.

Already saw Tip#5 in their website. So, in one request I have

postman.setGlobalVariable("mathjs", () => {
  \\ The full math.js library
});

Specifically, this is the code of math.js that I copied, in case the version matters.

Then in a request that is supposed to use the library I evaluate the global variable

eval(globals.mathjs)();

I don't use JavaScript often, so maybe it is something basic that I am missing. In the first request a global variable mahjs is defined, which value is a lambda that calls the code of the library. Then, in the second request, that lambda function is called. Please, correct me if my understanding so far is not correct.

Question: How does one call afterwards functions that were defined by the library?

I have tried: math.multiply(x,y);, Math.multiply(x,y);, multiply(x,y);. None of them are valid. The function multiply seems to be defined by the library and is used as math.multiply(array, matrix) .


Comparison with the reuse that I have already made work.

In one request

postman.setGlobalVariable("utils", () => {
  myfunction = function (x){
    return x+1;
  };
});

and in the request that uses it

eval(globals.utils)();
x = 1;
console.log(myfunction(x));

This works.

1

There are 1 best solutions below

1
Christian Baumann On BEST ANSWER

This solves your problem:

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);
});