Java Noob
Java Noob

Reputation: 371

Local Storage not Defined

I'm trying to use localstorage in javascript to save data between executions. Here is the code using it

const commando = require('discord.js-commando');
roasts = localStorage.getItem("roasts").split(/\r?\n/);

class RoastMe  extends commando.Command {
    constructor(client){
        super(client, {
            name:'addroast',
            group:'insult',
            memberName:'addroast',
            description:'Add a roast to the !roastme archive'
        });
    }

    async run(message, args){
        if(args.length==0) message.reply("That aint a roast.");
        else{
            roasts.push();
            roasts[roasts.length-1] = args;
            var save = "";
            for(var i = 0; i < roasts.length; i++){
                save += roasts[i] + "\n";
            }

            localStorage.setItem("roasts", save);
        }
    }
}
module.exports = RoastMe;

This is the error it produced when i tried to run it

ReferenceError: localStorage is not defined
at Object.<anonymous>

//location of error
(/home/ubuntu/workspace/commands/insult/addinsult.js:2:1)

at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at /home/ubuntu/workspace/node_modules/require-all/index.js:52:46
at Array.forEach (native)

If there's a better way to store data between executions, please let me know. Otherwise, could you explain why this isn't working? Thanks.

EDIT: The code posted is from addinsult.js

Upvotes: 2

Views: 34388

Answers (4)

Gihan Jayakuru
Gihan Jayakuru

Reputation: 101

faced same error in Angular app. works for me with:

typeof window !== 'undefined'

example:

initCartLocalStorage() {
    if (typeof window !== 'undefined') {
      const initCart = {
        items: [],
      };

      const initCartJSON = JSON.stringify(initCart);

      localStorage.setItem('cart', initCartJSON);
    }
  }

Upvotes: 0

Uxoos
Uxoos

Reputation: 21

maybe some there used

delete localStorage;

change to

localStorage.clear();

Upvotes: 0

Soldeplata Saketos
Soldeplata Saketos

Reputation: 3461

you need to declare it somewhere, like in the top of the js file, specifying it is global:

/* global localStorage, */

or, alternatively, use the version tied to window (it is your global object)

window.localStorage.setItem("roasts", save);

Upvotes: 1

Astrydax
Astrydax

Reputation: 465

Where are you defining local storage? If it's a module you need to 'require' it.

this line roasts = localStorage.getItem("roasts").split(/\r?\n/);

Is trying to do stuff with something called "localStorage", which has never been defined.

It looks like you're trying to use this module if i'm not mistaken: https://www.npmjs.com/package/node-localstorage I found these instructions on how to use it on that page.

if (typeof localStorage === "undefined" || localStorage === null) {
   var LocalStorage = require('node-localstorage').LocalStorage;
   localStorage = new LocalStorage('./scratch');
}

localStorage.setItem('myFirstKey', 'myFirstValue');
console.log(localStorage.getItem('myFirstKey'));

Upvotes: 2

Related Questions