Ask coding questions

← Back to all posts
JS Cookies SOLVED
GeneralBaker

I am trying to use cookies to store variables such as an integer (coins) and arrays (floors) but when I load the page they haven't stored anything.

What should happen: If you close the page, then reopen it, you coins and the skyscraper should the same as when you closed it.

Thank you:
@Coder100
@xxpertHacker
for your help!

Answered by xxpertHacker [earned 5 cycles]
View Answer
Voters
GalaxyWolfX
Comments
hotnewtop
xxpertHacker
GeneralBaker

It worked! I am successfully storing the amount of coins with very few bugs! Thanks for your help! @xxpertHacker

xxpertHacker

@GeneralBaker I'm surprised, some people have a hard(er) time using IndexedDB; where are you using it?

GeneralBaker

Worked like a charm. A charm that took several hours to learn how to use, but a charm nevertheless.

Side-note, but do you know how to store arrays using this? @xxpertHacker

GeneralBaker

Answer to you question above, In database.js
@xxpertHacker

xxpertHacker

@GeneralBaker Ah, I see, you're using a lib, "idb," last library that I used for it was idb-keyval; very simple, easy to use wrapper.

Yeah, it might take some time to learn IndexedDB itself, but afterward, it works very well, and libs make it much simpler. Just don't store classes directly, as they will be serialized into plain objects.

As for how you store arrays, I'm not sure about the specific library that you're using; you might be interested in trying out that lib ^, storing an array is as simple as writing await db.store("array", [ 1, 2, 3 ]) and const array = await db.get("array")

But it's great to hear that it helped, good luck!

Coder100

you can only store strings. You can use JSON.parse() to parse your arrays and objects and JSON.stringify() to convert to a string 'object'

Number() for your parsing number needs.

GeneralBaker

So use JSON.stringify(array) to convert the array to a string, then do JSON.parse(string) to convert string to an array?
Then do Number(num) to convert strings to numbers?
@Coder100

sammightyt

@GeneralBaker it's

sammightyt

@sammightyt I think?

Coder100

lol what @sammightyt

Coder100

yeah
use stringify to save, parse to parse, and Number to convert to number
@GeneralBaker

sammightyt

@Coder100 I'm bad at JSON :(

Coder100

lol you mean the json object @sammightyt

GeneralBaker

okee dokey, I put it in and now I got an error:
script error

#1 reason I dislike javascript is it doesn't tell you what is wrong and where its wrong. Coder100, could you check out the cookieReader.js file and give me a clue? Forgot to mention this before, but thanks for your help! @Coder100

Thanks for your help too @sammightyt ;)

Coder100

yes it does, you mean repl.it doesn't @GeneralBaker

Coder100

@GeneralBaker

here is your error

GeneralBaker

sigh I apologize javascript. I fixed the bug, but my cookies still don't store any information. Are you able to refresh the page and have the money still be not 10 [email protected]

Coder100

@GeneralBaker

your bug still is not fixed what

GeneralBaker

oooooooh whoops I forgot to mention that my floors[] is an array of objects, not numbers or strings. Would that affect the parsing (like would it parse to int or string or something instead of an object)

secondly, I didn't even get an error on repl.it, but from what it looks like it seems like the error is about what i said above. @Coder100

xxpertHacker

@GeneralBaker Stingifying functions won't work :/

Coder100

i like how people think they need to save functions @xxpertHacker

Coder100

@xxpertHacker

GeneralBaker

I'm not. Currently I am trying to convert a string to an array of objects. This is occuring in the cookieReader.js file if you would like to take a look. @xxpertHacker

GeneralBaker
Coder100

@GeneralBaker did you notice that floors are classes?
Add a method called asObject which returns a string object of whatev data you need saved, a lot of those things you probably won't need, like methosds.

xxpertHacker

@Coder100 You never know:

But they're using methods, so they just need a serialization/deserialization process and class good constructors.

Coder100

well in that case, maybe make a new lib called saveUrfunction

@xxpertHacker

GeneralBaker

did you notice that floors are classes? Add a method called asObject which returns a string object of whatev data you need saved, a lot of those things you probably won't need, like methosds.

Yes I did notice, but I wasn't sure how to do this.
@Coder100

Coder100

@GeneralBaker

GalaxyWolfX

Somewhat irrelevant: Line 36, 42, 47, 53- I would put the prices on the buttons:
[...createButton("1. Buy Small Gen- $10k)]
and so forth.

GeneralBaker

Done, though this is probably temporary. Did you notice the tooltips on the buttons? Those were going to be in a panel right next to the buttons when I get to that. Right now I am working on content ;)[email protected]

GalaxyWolfX

@GeneralBaker Oh, okay. Guess I just assumed you wouldn't get to it. My bad.