0

I am able to set global variable but unable to get that.

pm.sendRequest(getLibraries, function (err, response) {
    if(!err){
        var jsonData = response.json();
        console.log('Libraries: ',jsonData);
        var lib_id = jsonData.contents[0].id;
        console.log('Lib_id:', lib_id);
        pm.globals.set("MLSLibrary_id", lib_id);
    }
});
var library_id = pm.globals.get("MLSLibrary_id"); 
console.log('MLSLibrary_id: ', library_id);
Ranjeet
  • 49
  • 8

1 Answers1

0

The pm.sendRequest is asynchronous. Your request gets executed after you retrieve the global variable. If you want to execute something after the request do it in the if:

pm.sendRequest(getLibraries, function (err, response) {
    if(!err){
        var jsonData = response.json();
        console.log('Libraries: ',jsonData);
        var lib_id = jsonData.contents[0].id;
        console.log('Lib_id:', lib_id);

        // Execute your code with lib_id here
    }
});
Halil Bahar
  • 31
  • 1
  • 1