'Postman if/else condition not working properly

This code is not working as expected when running collection and used as a test script on the first request:

  1. when console.log gives output null, the collection doesn't stop, when it should.

  2. function never goes to the 'else' condition, even when I force it by changing the GET results.

     pm.test("Any?", function () {
     var jsonData = pm.response.json();
     if (pm.expect(jsonData.metadata.total_data).to.eql(0))
     {console.log('null');
     postman.setNextRequest(null);}
     else
     {console.log('Next request');
     postman.setNextRequest('Name of Next request');}
     });
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source