Get data from the local storage of a device or browser

Get small amounts of data previously saved to a users device or browser

This code example assumes that you’ve already saved data to the local device and know the name (ie, the key) that you gave it. If you haven’t done this please see an example of saving data locally here.

The code below attempts to get the data from a user’s mobile device or browser with the name of userData. If successful, the code in the first function block will be run. If there is an error (eg: no data was found) then the code in the second catch block will be run.

JavaScript

// Function to get data
function getUserData() {
  return Fliplet.App.Storage.get('userData');
}

getUserData()
  .then(function (data) {
    // Run after the data has been saved successfully
    Fliplet.UI.Toast({
      type: 'regular',
      title: 'Data retrieved',
      message: 'Your data is ready to be used.'
    });
  });

You can also get several pieces of data at once from the local storage like this:

JavaScript

// Function to get data
function getData(data) {
  return Fliplet.App.Storage.get(data);
}

// Get several pieces of data at once
getData(['userData', 'anotherKey'])
  .then(function (data) {
    // Run after the data has been saved successfully
    // data will by an Object containing the properties "userData" and "anotherKey"
    Fliplet.UI.Toast({
      type: 'regular',
      title: 'Data retrieved',
      message: 'Your data is ready to be used.'
    });
  });

In this second example, the data returned will be a single object containing the data and it’s corresponding name.

For further information and examples on how to use the local storage, please refer to this.