Can you add "[ ]" to variable?

So, I firstly use POST to create two users. Next, GET checks their id, so I have something like:
id: 19
id: 20

So I know those are two LAST ids of these users. At end, I want to delete them both. My pre-request script looks like:

let deleteid = pm.collectionVariables.get("deleteid");
pm.globals.get("LastDeviceID");
if(!deleteid || deleteid.length == 0) {
    deleteid = [null, "[555]", "abc", "f1r4.y6u8,o9", "{{LastDeviceID}}"];

But…I got response:
“errorMessage”: “Bad request: Cannot convert 20,19 to a BigInt”

because it has to be [20,19]. Or maybe [20,19,18,17]. How can I add “[ ]” at start and end of this variable?

Hi @michalbanas

It’s hard to tell what’s happening when it’s only a snippet of the code.
Maybe you could share the whole thing or a screenshot?

My first thought here is that {{LastDeviceID}} should not be inside " " because it would be reading that as a string, not passing the variable.

The full pre-request script:

let deleteid = pm.collectionVariables.get("deleteid");
pm.globals.get("LastDeviceID");
if (!deleteid || deleteid.length == 0) {
    deleteid = [null, "[555]", "abc", "f1r4.y6u8,o9", "{{LastDeviceID}}"];
}
let currentdeleteid = deleteid.shift();
pm.collectionVariables.set("delete_id", currentdeleteid);
pm.collectionVariables.set("deleteid", deleteid);

And in the Params tab, is the {{delete_id}} variable which checks for null, "[555]", "abc", "f1r4.y6u8,o9", "{{LastDeviceID}}". When it comes to "how I save 2 last variables, I used the code below at one of my GET method:

pm.test("Body has 'id' string", function () {
    pm.expect(pm.response.text()).to.include("id");
    const responseJson = pm.response.json();
    let arr = responseJson;
    let LastDeviceID = arr.length - 1;
    let LastDeviceID1 = arr.length - 2;
    let TwoUsers = [arr[LastDeviceID].id, arr[LastDeviceID1].id]
    pm.collectionVariables.set("LastDeviceID", TwoUsers);
    console.log(pm.collectionVariables.get("LastDeviceID"))
    console.log(pm.collectionVariables.get("LastDeviceID")[0])
}); 

You might be true, it will pass it as a string, but it could work nicely if I could somehow add “[” and “]” at end of that string because to make DELETE work, id must be in square brackets, either [20] or [20,19].

I think you’d be able to do something like;

let myArray = [null, "[555]", "abc", "f1r4.y6u8,o9", "["+LastDeviceID+"]"];

This "["+LastDeviceID+"]" should print [ as a character, then pass the variable +LastDeviceID+ and then print ] as a character.

(You can’t use the {{}} notation in pre-req so you would have to set the variable to a name first, eg;

let LastDeviceID = pm.globals.get("LastDeviceID");

Example output;

image

Didn’t work for me…

And console log says —> com/alpha/users?ids=[undefined]
While LastDeviceID was “32,31” at that moment (in collection’s Variables tab).

Maybe could I extract one of those values and just do 2x DELETEs?

EDIT:
/////////////////////
With your help I managed to finally do it. It should look like:

deleteid = [null, "[555]", "abc", "f1r4.y6u8,o9", 666, "["+"{{LastDeviceID}}"+"]"];

So I had to make it as “[”+“{{LastDeviceID}}”+“]”

1 Like