Skip to content
Sign upLog in
← Back to Community
More Client-Side, Server-Side Communication Questions
Profile icon
hacker
has Hacker Plan
RayhanADev

Heya, Ray again. I have another Server-Side, Client-Side problem that needs a bit of help. So I need to assign a variable “name” with information from localStorage so that it finds the correct document name in the database, but I don’t know how to send it to the function from client-side to server-side.

My relevant code (not sharing the Repl for security reasons):
Server Side Index.js

async function dbGetMultiplier(db) { //The “name” variable is missing, and I need to give it data from localStorage. const docRef = db.collection('users').doc(name); const doc = await docRef.get(); let output = []; let n = await doc.data(); output.push({ m1: n.multiplier1, m2: n.multiplier2, m3: n.multiplier3, m4: n.multiplier4, m5: n.multiplier5 }); return output; } app.get('/get-multipliers', async (_, res) => { res.send(await dbGetMultiplier(db)); });

Client Side Index.js

const data = (await fetch('/get-multipliers', { method: 'GET' }).then(r => r.json()))[0]; const m1 = data.m1; const m2 = data.m2; const m3 = data.m3; const m4 = data.m4; const m5 = data.m5; let totalMult = ((m1 * 10) + (m2 * 10) + (m3 * 10) + (m4 * 10) + (m5 * 10)) / 10;

That’s all the relevant information. Can someone help find the problem. Thanks,
~ Ray

Voters
Profile icon
RayhanADev
Comments
hotnewtop
Profile icon
Tonarda

Hey! I also recently tried to find good desk-alert software. It took me a lot of time to search, but in the end I found what I was looking for for so long, read this https://www.alert-software.com/blog/workplace-safety-communications .
With this application, it is much easier to monitor the work of employees and if there is something that needs to be notified to everyone, you can simply send a notification to the desktop. It is very comfortable!

Profile icon
RoninMartin

For business development it is very important to attract new customers and retain those that are there. To ensure that the client always has reliable support we use https://wow24-7.io/saas, our company's reputation is very important to us and that's why we choose the best outsourcing of customer support. Behind every big business there should be a strong saas support team.

Profile icon
TsunamiOrSumth

@RayhanADev
hi if ur not busy can you check out something i am making :D

Profile icon
RayhanADev

@TsunamiOrSumth
yeah sure why not. Invite me!

Profile icon
TsunamiOrSumth

@RayhanADev
i invited

Profile icon
CSharpIsGud
Profile icon
RayhanADev

@CSharpIsGud
gd, might I ask how you did this one xD? I swore I moved the multipliers.

Profile icon
CSharpIsGud

@RayhanADev
POST /save-db

Profile icon
RayhanADev

@CSharpIsGud
well um, can you help make this more secure xD.

Profile icon
CSharpIsGud

@RayhanADev
Not sure that's even possible, it has to give a score or tell when a good thing is clicked at some point and both ways can be cheated

Profile icon
RayhanADev

@CSharpIsGud
well then, don’t tell anyone what you know (I deleted like three versions of you but left one with leet xD). Um can you help with this Repl though, just like communication stuff.

Profile icon
CSharpIsGud

@RayhanADev
Anyone who knows what inspect element is already knows lol

Profile icon
RayhanADev

@CSharpIsGud
nono, can you help with server-side, client-side communication.

Profile icon
realTronsi

@Coder100
quick question, this isn't allowed right:
Screen Shot 2020-10-25 at 5.32.18 PM

Profile icon
Coder100

@realTronsi
wait what did he do

Profile icon
realTronsi

@Coder100
he posted the same post at least 4 times in talk already. (he deletes the other one and just reposts). I'm not sure if this is against ToS

Profile icon
Coder100

@realTronsi
are the reposts deleted?

Profile icon
realTronsi

@Coder100
yes, but same exact post, every few hours I come on I swear I see the post near/at the top of new talk, so he's been reposting very frequently

Profile icon
Coder100

@realTronsi
eh, seems like a strange way to get upvotes
but if he isn't like spamming the post, and instead silently spamming, mods can't really do anything about it

Profile icon
realTronsi

@Coder100
mmk, just a bit annoying since everytime I come on their tic tac toe game is on the top of the list

Profile icon
Coder100

ahhhhhh use the other function I gave you
and in your client side:

const data = (await fetch('/get-multipliers', { method: 'GET', headers: { 'content-type': "application/json" }, body: JSON.stringify({ name: <insert-the-name-variable-you-know> }) }).then(r => r.json()))[0]; const m1 = data.m1; const m2 = data.m2; const m3 = data.m3; const m4 = data.m4; const m5 = data.m5; let totalMult = ((m1 * 10) + (m2 * 10) + (m3 * 10) + (m4 * 10) + (m5 * 10)) / 10;

backend:

async function dbGetMultiplier(db, name) { //The “name” variable is missing, and I need to give it data from localStorage. // do something with it ig const docRef = db.collection('users').doc(name); const doc = await docRef.get(); let output = []; let n = await doc.data(); output.push({ m1: n.multiplier1, m2: n.multiplier2, m3: n.multiplier3, m4: n.multiplier4, m5: n.multiplier5 }); return output; } app.get('/get-multipliers', async (req, res) => { res.send(await dbGetMultiplier(db, req.body.name)); });
Profile icon
RayhanADev

@Coder100
it still throws an error? multiplier1 is not defined or something?

Profile icon
realTronsi

@RayhanADev
lol not sharing repl for security reasons? You know you have to share the repl to enter the jam

Profile icon
RayhanADev

@realTronsi
well...
fair enough. I’ll post it. Think you can help?

Profile icon
realTronsi

@RayhanADev
maybe, I need to look at the context. Also whats your "security reason"?

Profile icon
RayhanADev

@realTronsi
well the redeemable codes :D. Lol now you can see them.

Profile icon
realTronsi

@RayhanADev
lol whats the point of the codes? Also you can just store them in .env files if you want lmao

Profile icon
RayhanADev

@realTronsi
well can’t you see the .env if you look at the code editor xD.

Profile icon
realTronsi

@RayhanADev
no the whole point of env files is to store keys and secrets that others can't access

Profile icon
RayhanADev

@realTronsi
wait so rn you can’t see inside the .env file?

Profile icon
realTronsi

@RayhanADev
no, otherwise what is the point of .env files

Profile icon
RayhanADev

@realTronsi
wow, I serously misunderstand the code editor. Anyways, can you help? I posted the Repl in the answer.

Profile icon
realTronsi

@RayhanADev
this doesn't have anything to do with repl or the code editor, it is the literal definition/use of .env file

Profile icon
realTronsi

@RayhanADev
wait so whats your issue atm?

Profile icon
RayhanADev

@realTronsi
nono smh I’m talking about my communication problem (in the code). The one I asked in the question.

Profile icon
Coder100
Profile icon
realTronsi

@RayhanADev
whats the problem rn though? describe your issue other than "idk how"

Profile icon
RayhanADev

@realTronsi
okay right now my problem is an “n” variable that contains the data from a Firebase Document. I’m trying to get the value of a specific object on that document, but it returns something like Cannot read property multipier1 of undefined. The error could be something wrong in the way I set up the variable or it could be something else in the code. (Which is why I said IDK, I don’t want to confuse anyone who tries.) So um, does that make sense?

Profile icon
realTronsi

@RayhanADev
and where is this?

Profile icon
RayhanADev

@realTronsi
okay, look at index.js line 59 for the getMultiplier Function, index.js line 144 for the app.get, and public/js/index.js for the fetch.

Profile icon
realTronsi

@RayhanADev
you need better naming theres two index.js lol

Profile icon
RayhanADev

@realTronsi
it doesn’t effect anything I tested it xD.

Profile icon
realTronsi

@RayhanADev
btw you have a global variable named db and you also have a param named db, might cause problems idk

Profile icon
RayhanADev

@realTronsi
So you find the problem?

Profile icon
realTronsi

@RayhanADev
no idk but you have a global variable named db and a param named db, it probably wont affect anything but you should still rename to avoid confusing nodejs

Profile icon
RayhanADev

@Coder100
So I did what you said (a while back) and I started logging everything and req.body.name is undefined (in the app.get). That is the cause of all the errors! So my question is how do I fix it?

Profile icon
Coder100

@RayhanADev
is your post request invalid

Profile icon
Coder100

make sure the body is at least:

fetch('whatever', { body: { name: 'yeet' } })

@RayhanADev

Profile icon
RayhanADev

@Coder100
I don’t have a post request you didn’t say to...

Profile icon
Coder100
Profile icon
RayhanADev

@Coder100
yeah. Hop on the code you’ll see what I mean. I literally used what you put.