How to Update and Retrieve the Value of a Data Store Object using JavaScript?

This topic was automatically generated from Slack. You can find the original thread here.

how can I update the value of a data store object using JavaScript so that I can then retrieve the updated value the next time a step is run?

You can update the value of a data store object in a Pipedream component by using the this.data.set method. Here’s an example of how to update a value and retrieve it the next time the step is run: export default defineComponent({ props: { data: { type: "data_store" }, }, async run({ steps, $ }) { // Retrieve the value from the data store const currentValue = await this.data.get('myKey') ?? 0; // Update the value const newValue = currentValue + 1; await this.data.set('myKey', newValue); // The updated value will be available for retrieval in the next run }, }); In this example, we first retrieve the current value using this.data.get('myKey'). If the key doesn’t exist, we set a default value of 0. Then, we update the value and save it back to the data store using this.data.set('myKey', newValue). The updated value will be available for retrieval in the next run of the step.

When I run this code, it does not seem to update the values in the data stores dashboard - why is that?

I apologize for any confusion. The code I provided earlier should work for updating the value in the data store. However, please note that the data store values in the Pipedream dashboard may not update in real-time. It might take a few moments for the updated values to be reflected in the dashboard. If you still don’t see the updated values after waiting for a while, please double-check your code for any errors or issues. If you need further assistance, feel free to ask more questions or visit https://pipedream.com/support for more support options.

It’s been about an hour and the dashboard values are still not updating. Is this a bug?

I apologize for the inconvenience. It’s possible that there might be an issue with the code or the data store itself. To help you further, could you please share the complete code you’re using to update the data store? This will allow me to better understand the issue and provide a more accurate solution. Meanwhile, you can also visit https://pipedream.com/support for more support options.

Of course, here is the complete code:

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    data: { 
      type: "data_store",
      label: "Weekly Lineup Counters"  
    },
  },
  async run({ steps, $ }) {

    //Date formatting function
    function formatDate(dateString, timeInSeconds) {
      // Split the date string into year, month, and day components
      const [year, month, day] = dateString.split('-');
      // Create a new Date object in UTC using the components
      const date = new Date(Date.UTC(year, month - 1, day));
      // Convert the time from seconds to milliseconds
      const timeInMilliseconds = timeInSeconds * 1000;
      // Add the time to the date
      date.setUTCHours(7, 0, 0, timeInMilliseconds);
      // Format the date as "YYYY-MM-DD HH:MM:SS"
      const formattedDate = date.toISOString().replace('T', ' ').substr(0, 19);
      return formattedDate;
    }

    let fdb_image_counter = await this.data.get('fdb_image_counter');
    let fdb_subjectLine_counter = await this.data.get('fdb_subjectLine_counter');

    const fdbWeeklyImages = [
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_01.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_02.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_03.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_04.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_05.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_06.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_07.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_08.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_09.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_10.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_11.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_12.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_13.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_14.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_15.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_16.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_17.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_18.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_19.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_20.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_21.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_22.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_23.jpg"
    ];

    const fdbSubjectLines = [
                          "What to discover this week (for free). // Your personal recommendations.",
                          "Your recommended live classes. //Turn this week into anything.",
                          "Your personal recommendations. // Live, free, picked for your goals.",
                          "A week of live classes (that are free)  //  Your personal recommendations",
                          "7 days of live & free classes. // Your recommendations. On us. ",
                          "It's here! Your personal lineup. // Live. Free. Siri, add to calendar. ",
                          "Live this week. Recommended just for you. // Siri, please add to calendar.",
                          "Live this week Curated just for you. // Live classes we think you'll love.",
                          "Your weekly recommendations. // Live. Free. Personalized to you. ",
                          "NEW! Your free personal playlist. // Weekly recommendations to reach your goals.",
                          "Your personal playlist is in. // Live. Free. Recommended just for you.",
                          "Your weekly playlist just got personal. // Live, free, and now recommended just for you.",
                          "Your live recommendations are here! // Your recommendations. On the house.",
                          "Rediscover your weekly live classes. // Now with personal recommendations (and always free)",
                          "Max out on skills (for free!) with the Weekly Lineup//Personalized recommendations with your goals in mind",
                          "Fresh + just in. Your FREE Weekly Lineup dropped//And yup, these recommendations are personalized ",
                          "Change everything with free classes. Inside is your Weekly Lineup//These classes are tailored to you and all your goals",
                          "Create all week for free. Your Weekly Lineup is in//We hand-picked classes just for you",
                          "Free inspiration. Open up! Your Weekly Lineup is in//We hand-picked these classes, especially for you",
                          "Your creative breakthrough awaits. Use your free Weekly Lineup to get there//These personalized classes change everything",
                          "Learn something that changes everything. Peek your free Weekly Lineup//We curated these classes with you in mind",
                          "Push past your comfort zone with free classes from your Weekly Lineup//This list is hand-picked just for you",
                          "Stay inspired for free. Open up your Weekly Lineup//Open up for personalized recommendations",
                          "Free and oh-so fresh. This Weekly Lineup is everything//We picked this list just for you",
                          "Say hello to the Weekly Lineup. These classes are yours (for free!)//And chosen just for you",
                          "Yesss! Your Weekly Lineup is in and it is FILLED with free classes.//This list is for you, from us. Happy Creating.",
                          "Signed, sealed, delivered. This free Weekly Lineup is yours.//Every class was picked just for you",
                          "Free classes for you and your goals, all inside the Weekly Lineup//We picked every class especially for you",
                          "7 days of free classes. It's all inside the Weekly Lineup//We chose classes that would inspire you! Take a look.",
                          "Zone in on your goals with free classes from the Weekly Lineup//We picked the best classes, just for you",
                          "The Weekly Lineup is here and it's personal. (And free.)//We picked the best classes, just for you",
                          "FREE INSPIRATION. Your customized Weekly Lineup is in.//Especially for you, enjoy hand-picked classes",
                          "Take it to the limit. We have your free customized Weekly Lineup//And every class was picked especially for you",
                          "Future you says, "Thanks." Take a look at your free Weekly Lineup//Every class was picked just for you",
                          "You are THIS close to your goals. Go further with your free Weekly Lineup//Every class was picked just for you",
                          "This Weekly Lineup is ready for you (and all the classes are free!)//Enjoy your personalized recs all week!",
                          "Discover your new Weekly Lineup filled with free classes//Take a peek at your personalized recommendations",
                          "Your (soon to be) favorites are free + inside the Weekly Lineup//Your personalized recs are in, so take a look",
                          "The new Weekly Lineup just landed – Check out your free classes//Personalized recs are coming up",
                          "Cool off with our hot Weekly Lineup + free classes just for you//These recs just got personal"]

    // Find the date of the next Sunday after the trigger date and format it in "YYYYMMDD" format
    const triggerDate = new Date();
    const nextSunday = new Date(triggerDate.setDate(triggerDate.getDate() + (7 - triggerDate.getDay() || 7)));
    const campaignNameDate = nextSunday.toISOString().slice(0,10).replace(/-/g, "");

    let campaignObject = {
      campaignName: campaignNameDate + "_FDB_WeeklyLineup",
      image: fdbWeeklyImages[fdb_image_counter],
      subjectLine: fdbSubjectLines[fdb_subjectLine_counter]?.split("//")[0],
      preheader: fdbSubjectLines[fdb_subjectLine_counter]?.split("//")[1],
      send_datetime: formatDate(nextSunday.toISOString().slice(0,10), 22500),
      send_mode: "ProjectTimeZone",
      sender_email: "[learn@creativelive.com](mailto:learn@creativelive.com)",
      sender_name: "CreativeLive",
      send_lists: [840802, 228420],
      utm_params: [
        {
          "key": "utm_source",
          "value": "creativeLIVE"
        },
        {
          "key": "utm_medium",
          "value": "email"
        },
        {
          "key": "utm_campaign",
          "value": campaignNameDate + "_FDB_WeeklyLineup"
        }
      ],
    }

    if (fdb_image_counter >= fdbWeeklyImages.length) {
      await this.data.set('fdb_image_counter', 0);
    } else {
      await this.data.set('fdb_image_counter', fdb_image_counter++);
    }

    if (fdb_subjectLine_counter >= fdbSubjectLines.length) {
      await this.data.set('fdb_subjectLine_counter', 0);
    } else {
      await this.data.set('fdb_subjectLine_counter', fdb_subjectLine_counter++);
    }
    console.log('Image Counter: ',fdb_image_counter)
    console.log('Subject Counter: ', fdb_subjectLine_counter)
    return campaignObject
  },
})

when I run this code, it does not reliably update the values in the ‘Weekly Lineup Counters’ data store. Can you please explain why and suggest a solution?

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    data: { 
      type: "data_store",
      label: "Weekly Lineup Counters"  
    },
  },
  async run({ steps, $ }) {

    //Date formatting function
    function formatDate(dateString, timeInSeconds) {
      // Split the date string into year, month, and day components
      const [year, month, day] = dateString.split('-');
      // Create a new Date object in UTC using the components
      const date = new Date(Date.UTC(year, month - 1, day));
      // Convert the time from seconds to milliseconds
      const timeInMilliseconds = timeInSeconds * 1000;
      // Add the time to the date
      date.setUTCHours(7, 0, 0, timeInMilliseconds);
      // Format the date as "YYYY-MM-DD HH:MM:SS"
      const formattedDate = date.toISOString().replace('T', ' ').substr(0, 19);
      return formattedDate;
    }

    let fdb_image_counter = await this.data.get('fdb_image_counter');
    let fdb_subjectLine_counter = await this.data.get('fdb_subjectLine_counter');

    const fdbWeeklyImages = [
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_01.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_02.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_03.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_04.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_05.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_06.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_07.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_08.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_09.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_10.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_11.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_12.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_13.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_14.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_15.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_16.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_17.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_18.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_19.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_20.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_21.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_22.jpg",
    "https://pacayadigital-creativelive.s3.us-west-2.amazonaws.com/weekly-lineup/FDB/FDB_WeeklyLineup_Heros_23.jpg"
    ];

    const fdbSubjectLines = [
                          "What to discover this week (for free). // Your personal recommendations.",
                          "Your recommended live classes. //Turn this week into anything.",
                          "Your personal recommendations. // Live, free, picked for your goals.",
                          "A week of live classes (that are free)  //  Your personal recommendations",
                          "7 days of live & free classes. // Your recommendations. On us. ",
                          "It's here! Your personal lineup. // Live. Free. Siri, add to calendar. ",
                          "Live this week. Recommended just for you. // Siri, please add to calendar.",
                          "Live this week Curated just for you. // Live classes we think you'll love.",
                          "Your weekly recommendations. // Live. Free. Personalized to you. ",
                          "NEW! Your free personal playlist. // Weekly recommendations to reach your goals.",
                          "Your personal playlist is in. // Live. Free. Recommended just for you.",
                          "Your weekly playlist just got personal. // Live, free, and now recommended just for you.",
                          "Your live recommendations are here! // Your recommendations. On the house.",
                          "Rediscover your weekly live classes. // Now with personal recommendations (and always free)",
                          "Max out on skills (for free!) with the Weekly Lineup//Personalized recommendations with your goals in mind",
                          "Fresh + just in. Your FREE Weekly Lineup dropped//And yup, these recommendations are personalized ",
                          "Change everything with free classes. Inside is your Weekly Lineup//These classes are tailored to you and all your goals",
                          "Create all week for free. Your Weekly Lineup is in//We hand-picked classes just for you",
                          "Free inspiration. Open up! Your Weekly Lineup is in//We hand-picked these classes, especially for you",
                          "Your creative breakthrough awaits. Use your free Weekly Lineup to get there//These personalized classes change everything",
                          "Learn something that changes everything. Peek your free Weekly Lineup//We curated these classes with you in mind",
                          "Push past your comfort zone with free classes from your Weekly Lineup//This list is hand-picked just for you",
                          "Stay inspired for free. Open up your Weekly Lineup//Open up for personalized recommendations",
                          "Free and oh-so fresh. This Weekly Lineup is everything//We picked this list just for you",
                          "Say hello to the Weekly Lineup. These classes are yours (for free!)//And chosen just for you",
                          "Yesss! Your Weekly Lineup is in and it is FILLED with free classes.//This list is for you, from us. Happy Creating.",
                          "Signed, sealed, delivered. This free Weekly Lineup is yours.//Every class was picked just for you",
                          "Free classes for you and your goals, all inside the Weekly Lineup//We picked every class especially for you",
                          "7 days of free classes. It's all inside the Weekly Lineup//We chose classes that would inspire you! Take a look.",
                          "Zone in on your goals with free classes from the Weekly Lineup//We picked the best classes, just for you",
                          "The Weekly Lineup is here and it's personal. (And free.)//We picked the best classes, just for you",
                          "FREE INSPIRATION. Your customized Weekly Lineup is in.//Especially for you, enjoy hand-picked classes",
                          "Take it to the limit. We have your free customized Weekly Lineup//And every class was picked especially for you",
                          "Future you says, "Thanks." Take a look at your free Weekly Lineup//Every class was picked just for you",
                          "You are THIS close to your goals. Go further with your free Weekly Lineup//Every class was picked just for you",
                          "This Weekly Lineup is ready for you (and all the classes are free!)//Enjoy your personalized recs all week!",
                          "Discover your new Weekly Lineup filled with free classes//Take a peek at your personalized recommendations",
                          "Your (soon to be) favorites are free + inside the Weekly Lineup//Your personalized recs are in, so take a look",
                          "The new Weekly Lineup just landed – Check out your free classes//Personalized recs are coming up",
                          "Cool off with our hot Weekly Lineup + free classes just for you//These recs just got personal"]

    // Find the date of the next Sunday after the trigger date and format it in "YYYYMMDD" format
    const triggerDate = new Date();
    const nextSunday = new Date(triggerDate.setDate(triggerDate.getDate() + (7 - triggerDate.getDay() || 7)));
    const campaignNameDate = nextSunday.toISOString().slice(0,10).replace(/-/g, "");

    let campaignObject = {
      campaignName: campaignNameDate + "_FDB_WeeklyLineup",
      image: fdbWeeklyImages[fdb_image_counter],
      subjectLine: fdbSubjectLines[fdb_subjectLine_counter]?.split("//")[0],
      preheader: fdbSubjectLines[fdb_subjectLine_counter]?.split("//")[1],
      send_datetime: formatDate(nextSunday.toISOString().slice(0,10), 22500),
      send_mode: "ProjectTimeZone",
      sender_email: "[learn@creativelive.com](mailto:learn@creativelive.com)",
      sender_name: "CreativeLive",
      send_lists: [840802, 228420],
      utm_params: [
        {
          "key": "utm_source",
          "value": "creativeLIVE"
        },
        {
          "key": "utm_medium",
          "value": "email"
        },
        {
          "key": "utm_campaign",
          "value": campaignNameDate + "_FDB_WeeklyLineup"
        }
      ],
    }

    if (fdb_image_counter >= fdbWeeklyImages.length) {
      await this.data.set('fdb_image_counter', 0);
    } else {
      await this.data.set('fdb_image_counter', fdb_image_counter++);
    }

    if (fdb_subjectLine_counter >= fdbSubjectLines.length) {
      await this.data.set('fdb_subjectLine_counter', 0);
    } else {
      await this.data.set('fdb_subjectLine_counter', fdb_subjectLine_counter++);
    }
    console.log('Image Counter: ',fdb_image_counter)
    console.log('Subject Counter: ', fdb_subjectLine_counter)
    return campaignObject
  },
})