Need help on Global variables

I am working on to integrate Google Distance Matrix API to get the Distance a time between two locations. I am doing the following steps:

  • Using WiX address Input, I am getting the TO and FROM address from webpage

  • Storing the value into session storage for further use.

  • When I am storing the value session storage, it is a string. But while reteriving I am getting an object in return. Below is my code snippet:

  • Code Snippet while storing the value:

session.setItem("pickupaddrvalue", $w("#pickupaddr").value);
    console.log($w("#pickupaddr").value);
  • Console Output

Pick Up address = [object Object]{formatted: “Auckland, New Zealand”, location: {…}, subdivision: “Auckland”, city: “Auckland”, country: “NZ”}

  1. city: “Auckland”

  2. country: “NZ”

  3. formatted: “Auckland, New Zealand”

  4. location: {latitude: -36.8484597, longitude: 174.7633315}

  5. subdivision: “Auckland”

  6. proto: Object

  • Getting the value from storage
let pickupaddrvalue = session.getItem("pickupaddrvalue");
console.log("Pick Up address = " + pickupaddrvalue)
  • while performing the get I am getting an object rather than a text/string:
Pick Up address = [object Object]

Please help, If I am doing anything wrong here to receive a string.

@Raman MITTAL,

Use JSON.stringify to write the object to storage and JSON.parse to convert the string to a Javascript object that you can use.

let addrObj = JSON.stringify($w("#pickupaddr").value);
session.setItem("pickupaddrvalue", addrObj);
let pickupaddrObj = JSON.parse(session.getItem("pickupaddrvalue"));