Disadvantage with localStorage

0

I'm doing an application on Ionic 3, linked to a php-slim-Restful. In this application, when you login, link it via POST to the URL of the php-slim-resful. I used the following function so that in the console I keep the data (object) and thus use it:

localStorage.setItem('userData', JSON.stringify(this.responseData) )

When logging out I put the LocalStorage.clear () function; so that the data with which I had logged in was cleaned. The problem here is that from my welcome screen when I click on "Go to start session" this error is sent to me:

  

Error: Uncaught (in promise): TypeError: data is null   c @ http: // localhost: 8100 / build / polyfills.js: 3: 13535 or / < @http: // localhost: 8100 / build / polyfills.js: 3: 12891 NavControllerBase.prototype._fireError @ link ost: 8100 / build / vendor.js: 43269: 13 NavControllerBase.prototype._failed@http: // localhost: 8100 / build / vendor.js: 43257: 9 NavControllerBase.prototype._nextTrns / < @http: // localhost: 8100 / build

The truth is that I do not know why. Someone could help me? It should be noted that after I entered the method localstorage.clear (); That mistake came to me.

    
asked by Elías 31.08.2017 в 02:37
source

1 answer

0

I assume that the error started to appear when you entered the sentence to clean the localStorage , because if it is not, it is difficult to know if one is related to the other without seeing the rest of the code:

When you call localStorage.getItem('userData') , if the item does not exist, you get null . You may do something like

let userData=JSON.parse(localStorage.getItem('userData'));

and you are not checking if the variable is null before using it during login.

Anyway, it would be easier to check the error if the login code was added to the question (it seems to be a problem when resolving a Promise, so I assume it is something that happens after making an AJAX request to the server

    
answered by 31.08.2017 в 11:10