+ 6

Storing values returned by a promise

Hi! How to Store values returned by a promise. I tried to find the answer on sololearn and google. But none of them worked. Here is the problem. I m using firebase for a project. I have a key called users in it which stores user information. But every time I login the data is updated. So I decided to test if the user is there before updating. THIS IS THE CODE I USED IN THAT CASE. ... let hasUser; firebase.database().ref("/users").once("value").then((snap)=>{ //1st try.(Not working at all) //user is a variable which stores the user name which is a key in users object //return snap.hasChild(user); //2nd try(Working only in CLI but not in normal way) hasUser = snap.hasChild(user); }); if(!hasUer) { //storing the new user } else { //default } So my question is how to store a value returned by a promise or use a promise inside a if statement, and what I have done wrong. Thanks and Happy new year!!!

2nd Jan 2019, 12:16 PM
Seniru
Seniru - avatar
11 Respostas
+ 8
Seniru Pasan i meant setup a new firebase project so it can be tested No biggie, i made one myself, and then discovered that you can't use the google login provider in SL playground So i just set up a small demo without it, filling out email username and password and checking if the user exists https://code.sololearn.com/WWicfvY1WHWf/?ref=app i am a bit rusty with firebase tho :/
4th Jan 2019, 11:35 AM
Burey
Burey - avatar
+ 8
Seniru Pasan can you set up a test project in firebase to play with? And if you want to access something inside the promise scope just assign it to a variable before the promise and you can access it there
3rd Jan 2019, 12:13 PM
Burey
Burey - avatar
+ 7
Seniru Pasan can you post the code on the playground to play with?
2nd Jan 2019, 5:36 PM
Burey
Burey - avatar
+ 5
Burey how to make a test project? And storing variables in the promise wont work. Any examples please ☺
3rd Jan 2019, 12:15 PM
Seniru
Seniru - avatar
+ 4
Burey Paul r8w9 Morpheus Calviղ can you help me? Thanks 😊
2nd Jan 2019, 12:44 PM
Seniru
Seniru - avatar
+ 3
Burey i ll try to post it here. But before that I found the promise has a local scope, so anything inside it cannot access outside. So I decided to include if statement inside promise.And its working. Is it good or is there another way to it?
3rd Jan 2019, 10:21 AM
Seniru
Seniru - avatar
+ 3
Döwlet Hallayew I think I have written hasUser correctly in the if statement. And if the user is in database hasUser will be evaluated to false - yes you are right. But a code in else statement says that the user is already signed in
18th May 2019, 3:09 AM
Seniru
Seniru - avatar
4th Jan 2019, 2:09 AM
🇮🇸🇭🇮🇰🇦©
🇮🇸🇭🇮🇰🇦© - avatar
0
why my code is not working see in my profile plz
4th Jan 2019, 9:36 AM
Umesh Dhakal
Umesh Dhakal - avatar
0
Did you copy the code or wrote it down? Because in the if statement it says "hasUer", while you declared it as "hasUser". And in my opinion, when you write "!hasUser" It will return false if it has the user, thus the code in the else statement will run? Or correct me if i'm wrong 😅
17th May 2019, 5:07 PM
Dovlet Hallayev