Skip to content
Advertisement

Javascript list_pop on Zapier Storage

I’m a heavy user of the Zapier Store in code blocks (Javascript).

Recently the data sent to the storage encountered a sudden increase and sometimes I got a full store (cleaning it everyday does not help).

I wanted to adopt a FIFO approach, for which if the number of records in the store > 450, I would delete the first record created in the Store.

I’ve been trying the following:

let secret = "mySuperSecret";
let store = StoreClient(secret);

// Get the amount of data included in the Storage
let res = await fetch("https://store.zapier.com/api/records?secret=" + secret);
let body = await res.json();
let length= Object.keys(body).length;
console.log(length);

let value = await store.get(inputData.importantInfo);
if (value == "published"){
  found = true;
  callback(null, {result:"Store Record already found"});
} else {
    // Check if the number of records is higher than the limit
    if (length>450){
      // Delete the first record in the Storage (FIFO)
      store.list_pop('userMail', location='head') // Does not work
    }
    // Set the new storage value
    store.list_push(inputData.importantInfo, "published");
    callback(null, {result:"Storage value (" + inputData.importantInfo + ") set to 'published'"});
}

But I had no success. I guess the list_pop method is for Python only.

Did anybody try something like this and found the solution?

Does anybody have a better idea?

Thanks a lot!

Advertisement

Answer

I solved it: the problem was in the way the storage was set up and in the need for a PATCH request.

To add elements:

let url = "https://store.zapier.com/api/records";
let headers = {
        "Content-Type":"application/json",
        "X-Secret": secret
    }

body = {
    "action":"list_push",
    "data": 
    {
        "key":key,
        "value":value
    }
};

let options = {
    "method": "PATCH",
    "headers": headers,
    "body": JSON.stringify(body)
    }

let response = await fetch(url, options);

You will get a repository that looks like this:

{"group": {"list": ["value1", "value2", "value3"...]}}

To pop elements from the top of the list:

let url = "https://store.zapier.com/api/records";
let headers = {
        "Content-Type":"application/json",
        "X-Secret": secret
    }

body = {
    "action":"list_pop",
    "data": 
    {
        "key":key,
        "location":"head"
    }
};

let options = {
    "method": "PATCH",
    "headers": headers,
    "body": JSON.stringify(body)
    }

let response = await fetch(url, options);

Thanks!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement