Question

List Pop on Zapier Store with Javascript

  • 31 March 2022
  • 4 replies
  • 113 views

Userlevel 1

Hello everyone!

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 found = false;
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!


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 replies

Userlevel 7
Badge +9

Thanks for looping back around to let us know worked for you, @JuliAudroc! We love to see it. 🎉

Appreciate you jumping in to share another alternative solution too, @ikbelkirasan! 🤗 

Userlevel 7
Badge +12

Hi @JuliAudroc - Instead of list_push and list_pop, you should call listPush and listPop. Hope this helps.

Userlevel 1

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!

Userlevel 7
Badge +12

Hi @JuliAudroc!

This code is a little beyond where I would be able to comment, but I know that @ikbelkirasan and @Troy Tessalone have quite a bit of experience in using code steps, so I wonder if they would be able to help you here.