I'm trying to save these IDs, but they are not getting saved to my environment variables and I'm seeing the console output as:
undefined
undefined
Json Response
{
"savedIds": [
95672,
95673
]
}
Test
for(let i = 0; i< jsonData.savedIds.length; i++) {
pm.environment.set("savedID" + [i+1],jsonData)
console.log(pm.environment.get('savedIds' + [i+1]));
};
Firstly, your code to get JSON data works well such as written here:
let jsonData = {
"savedIds": [
95672,
95673
]
};
for(let i = 0; i < jsonData.savedIds.length; i++) {
console.log(i + ", " + jsonData.savedIds[i]);
};
The issue come from the key used with Postman. You are using savedID then savedIds. So, you will do something like this:
for(let i = 0; i< jsonData.savedIds.length; i++) {
pm.environment.set("savedID" + [i+1], jsonData.savedIds[i])
console.log(pm.environment.get('savedID' + [i+1]));
};
Just as an alternative way of doing the same thing:
jsonData.savedIds.forEach((id, index) => pm.environment.set(`savedID_${index + 1}`, id));
or using Lodash in Postman:
_.each(jsonData.savedIds, (id, index) => pm.environment.set(`savedID_${index + 1}`, id));