Search code examples
javascriptjqueryarrayslocal-storageamplifyjs

AmplifyJS not storing JavaScript array in local storage


I am experiencing an issue with AmplifyJS where I am storing an Array of Arrays into local storage, set with the key of product_array_to_add and when I then try and access it with:

var prod_array = amplify.store('product_array_to_add');

I get an empty array.

// this code works
var product_ids = amplify.store('product_ids_to_add').split(':');
console.log(product_ids);

// both console output statements produce empty arrays
var prod_array = amplify.store('product_array_to_add'); 
console.log(prod_array);
console.dir(prod_array);

The below code is how I am setting the values:

amplify.store('product_ids_to_add', product_ids);
amplify.store('product_array_to_add', ProductsArr);

Does AmplifyJS have an issue with storing an Array of Arrays? Is there any other reason why I can't access the array?


Solution

  • I have since found that the problem was down to the fact that I wasn't pushing JS objects into the array. So I changed my code to do exactly that and Amplify was quite happy to allow me to retrieve the data at the other end.

    var ProductsArr = new Array();
    
    $.each($("#container .product"), function(){
    
       productObj = {       
          name: $(this).find('.prod_name').text(),
          code: $(this).find('.prod_code').text(),
          price: parseInt($(this).find('.prod_price').text()),
          prod_id: $(this).find('.size_select').val()                       
       };
    
       ProductsArr.push(productObj);        
    
    });
    
    amplify.store('product_array_to_add', ProductsArr);