error receiving user id
Unanswered
subscr posted this in #help-forum
subscrOP
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
i'm using next-auth
58 Replies
subscrOP
...
Double-striped Thick-knee
I feel like the userId inside setTimeout is the old userId when it was undefiend.
subscrOP
how i can solve it?
Double-striped Thick-knee
add condition that won't call setTimeout if userId is undefined
if (!userId) return
subscrOP
but i need this userId
regardless, I have it
Double-striped Thick-knee
even if it's undefined?
Double-striped Thick-knee
what happens if you put this code in front of setTimeout
subscrOP
it stops at this if
Double-striped Thick-knee
why is it not printing
subscrOP
¯\_(ツ)_/¯
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
subscrOP
Double-striped Thick-knee
where does that userId comes from
Double-striped Thick-knee
when do you call load function
subscrOP
when the person enters the website
Double-striped Thick-knee
are you calling it when userId is undefined and then never calling it again?
subscrOP
yes
I needed the id before this "ready with token"
Double-striped Thick-knee
why aren't you using useEffect
subscrOP
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,
subscrOP
hmm
like this?
Double-striped Thick-knee
yeah
subscrOP
not worked
Double-striped Thick-knee
log inside useEffect
subscrOP
Double-striped Thick-knee
can you show the component
subscrOP
when I enter the id manually, it works
subscrOP
.
Hi @subscr Have you solved this issue?
I wanna help you
subscrOP
nope
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]); // Add
userIdto 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.
This could happen if userId is changing or being reset between the useCallback declaration and the setTimeout.
Hello
Are you working well?
subscrOP
when I get home I test
sorry for the delay, it's been very busy here
subscrOP
hm
and how i can get the new userId value?