Next.js Discord

Discord Forum

error receiving user id

Unanswered
subscr posted this in #help-forum
Open in Discord
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

59 Replies

...
@subscr 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
Double-striped Thick-knee
I feel like the userId inside setTimeout is the old userId when it was undefiend.
@subscr how i can solve it?
Double-striped Thick-knee
add condition that won't call setTimeout if userId is undefined
if (!userId) return
but i need this userId
regardless, I have it
@subscr but i need this userId
Double-striped Thick-knee
even if it's undefined?
@Double-striped Thick-knee even if it's undefined?
idk, this is my doubt
as you can see, in the first console, it returns the id
@Double-striped Thick-knee if (!userId) return
Double-striped Thick-knee
what happens if you put this code in front of setTimeout
Double-striped Thick-knee
why is it not printing
¯\_(ツ)_/¯
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
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
@subscr Click to see attachment
Double-striped Thick-knee
where does that userId comes from
I needed the id before this "ready with token"
Double-striped Thick-knee
when do you call load function
when the person enters the website
Double-striped Thick-knee
are you calling it when userId is undefined and then never calling it again?
yes
@subscr Click to see attachment
I needed the id before this "ready with token"
Double-striped Thick-knee
why aren't you using useEffect
because useEffect only loads once, when the page opens, right?
Double-striped Thick-knee
you have dependency array if you want to run it multiple times,
hmm
like this?
Double-striped Thick-knee
yeah
not worked
Double-striped Thick-knee
log inside useEffect
Double-striped Thick-knee
can you show the component
when I enter the id manually, it works
.
Hi @subscr Have you solved this issue?
I wanna help you
@Harry I wanna help you
please
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.
Hello
Are you working well?
apologies for the delay
I'll test it soon
here, it's lunch time
when I get home I test
sorry for the delay, it's been very busy here
.