Next.js Discord

Discord Forum

error receiving user id

Unanswered
subscr posted this in #help-forum
Open in Discord
Avatar
Guys, I wanted to know how I can resolve this error, the socket provider Id returns the normal id, but console.log 2 returns undefined

i'm using next-auth
Image
Image

58 Replies

Avatar
...
Avatar
Double-striped Thick-knee
I feel like the userId inside setTimeout is the old userId when it was undefiend.
Avatar
how i can solve it?
Avatar
Double-striped Thick-knee
add condition that won't call setTimeout if userId is undefined
if (!userId) return
Avatar
but i need this userId
regardless, I have it
Avatar
Double-striped Thick-knee
even if it's undefined?
Avatar
idk, this is my doubt
as you can see, in the first console, it returns the id
Image
Avatar
Double-striped Thick-knee
what happens if you put this code in front of setTimeout
Avatar
it stops at this if
Avatar
Double-striped Thick-knee
why is it not printing
Image
Avatar
¯\_(ツ)_/¯
in fact it logs in, first of all, in this case it is undefined, because the socketprovider id takes a while to receive the id
that's why the set timeout
Avatar
Double-striped Thick-knee
I don't think setTimeout is helping here. can you comment the setTimeout and log userId inside useCallback and show me the logs
Avatar
Image
Image
Avatar
Double-striped Thick-knee
where does that userId comes from
Avatar
I needed the id before this "ready with token"
next-auth
Image
Avatar
Double-striped Thick-knee
when do you call load function
Avatar
when the person enters the website
Avatar
Double-striped Thick-knee
are you calling it when userId is undefined and then never calling it again?
Avatar
yes
I needed the id before this "ready with token"
Avatar
Double-striped Thick-knee
why aren't you using useEffect
Avatar
because useEffect only loads once, when the page opens, right?
Avatar
Double-striped Thick-knee
you have dependency array if you want to run it multiple times,
Avatar
hmm
like this?
Image
Avatar
Double-striped Thick-knee
yeah
Avatar
not worked
Image
Avatar
Double-striped Thick-knee
log inside useEffect
Avatar
Image
Image
Avatar
Double-striped Thick-knee
can you show the component
Avatar
Image
when I enter the id manually, it works
Avatar
.
Avatar
Hi @subscr Have you solved this issue?
I wanna help you
Avatar
nope
please
Avatar
const load = useCallback(() => { console.log("1", userId); setTimeout(() => { console.log("2", userId); // userId should now refer to the latest value connect(userId).then((s) => { loadSocket(s); setReady(true); }); }, 3000); }, [userId]); // AdduserIdto the dependency array
The issue you're facing seems to be that the userId is undefined when the setTimeout function executes, but it works fine when logging right before the setTimeout.
This could happen if userId is changing or being reset between the useCallback declaration and the setTimeout.
Avatar
Hello
Are you working well?
Avatar
apologies for the delay
I'll test it soon
here, it's lunch time
Avatar
when I get home I test
sorry for the delay, it's been very busy here
Avatar
hm
and how i can get the new userId value?