lmaccherone / node-localstorage

A drop-in substitute for the browser native localStorage API that runs on node.js.
MIT License
450 stars 36 forks source link

Data being pulled in debug mode but not in live mode #31

Closed dougtaylor-cb closed 6 years ago

dougtaylor-cb commented 6 years ago

I'm using the code below in my node server. The first module is what handles the GET request, and the second one deals with localStorage. If I run the server in debug mode, everything works fine. Data is pulled from localStorage and then passed on to the source that made the GET request. If I run the server in live mode, localStorage fails to return any data at all. In this case, return false; is not called, nor is the catch block triggered, and even console.log('the localstorage if statement failed to run'); isn't called. The data being pulled from localStorage is about 1.5mb.

For example, if I'm in debug mode and I trigger the getTime function, my console shows this:

Getting time from storage
pulling from storage
data has been pulled
getTime:  2018-02-09T14:12:05.141Z

If I run it in live and do the same thing, I get this:

Getting time from storage
pulling from storage
getTime:  undefined

I tried making these async functions and using await, under the assumption that the problem was a delay in pulling the data from the file used for storage, but had the same results then.

var storage = require('../services/storage');

module.exports = {
  get:  function(req, res){
    console.log('request received.');
    let data = getClientData();      
    let clientData = JSON.parse(data);
    if (clientData){
      console.log('returning data.');
      res.send(clientData);
    }
  }, 
  getTime:  function(req, res){
    console.log('Getting time from storage');
    let data = getClientData(); 
    let time = JSON.parse(data).time;
    console.log('getTime: ', time);
    res.send(time);
  }
}

 function getClientData(){
  return storage.get('clientData');
}
|| localStorage === null) {
  var LocalStorage = 
    require('node-localstorage').LocalStorage;
  localStorage = new LocalStorage('./scratch');
}

module.exports = {
  get:  function(key){
    try {
      console.log('pulling from storage');
      let data = localStorage.getItem(key);
      if (data !== null && data !== ''
      && typeof data !== 'undefined') {
        console.log('data has been pulled');
        return data;
      } else {
        return false;
      }
    } catch (error) {
      console.error(
        'Unable to get ' + key + ' from localStorage!'
      );
      console.log(error);
    }
    console.log('the localstorage if statement failed to run');
  },
  set: function(key, value){
    try {
      console.log('Saving to localStorage');
      let objectToStore = {
        data: value,
        time: new Date()
      };
      let stringToStore = 
        JSON.stringify(objectToStore);
      localStorage.setItem(
        key, 
        stringToStore
        );
    } catch (error) {
      console.error(
        'Error while saving to localStorage!'
      );
      console.error(error);
    }
  }
}
dougtaylor-cb commented 6 years ago

I found the problem.

The node-localstorage module uses a different location in debug mode. In debug mode, it was saving to the same path as the package.json file, but in live mode it was using the path of the server.js file.

The reason why I was getting an empty string as a response is because the data I was testing with had been built in debug mode, and did not exist in the path the live mode was using.

Building the data in live mode resolved the issue, but this may be something you want to look into further.

lmaccherone commented 6 years ago

Hmmm, glad you figured it out. You can also specify the location when you instantiate.

localStorage = new LocalStorage('./scratch')