0

What is a way to check if a user exists at a firebase location? Would you use .child() somehow? A variable would store the user name entered:

user name:

var user = "test";

firebase returned object:

snapshot { '
   -JruhOP3M496hEAKn6': 
   { 
      email: 'chaselester.cl@gmail.com',
      password: 'kimberlyn#1',
      username: 'chipe' 
   },
  '-Ju8J72xo3UpSIwEYw': { 
      email: 'test', 
      password: 'test', 
      username: 'test' },
  '-Ju8J9m_DC18irjWep': { 
      email: 'one', 
      password: 'three', 
      username: 'two' 
  } 
}

Update: code from comment

It seems if the user does not exist firebaseRef.push calls firebaseRef.orderByChild and the whole function runs again because in the log it logs "User does not exist" then right after it logs "User Exists" and my res.json is called twice and I get an app crash with the comment "Can't set headers after they are sent". How can I push to firebase if the user does not exist without having it re evaluate the firebaseRef.orderByChild?

Here is the full function I am doing in my server.js:

firebaseRef.orderByChild('username').equalTo(user).on('value', function(snapshot) { 
  if (snapshot.hasChildren()) { 
    console.log('User exists'); 
    res.json(true); 
  }else if(!snapshot.hasChildren()){ 
    console.log('User does not exist'); 
    firebaseRef.push(req.body); 
    res.json(false); 
  } 
}); 
Chipe
  • 4,641
  • 10
  • 36
  • 64
  • 1
    `ref.orderByChild('username').equalTo(user).on('value', function(snapshot) { if (snapshot.hasChildren()) { console.log('User exists'); })` – Frank van Puffelen Jul 13 '15 at 22:10
  • Awesome, that works. I am in the server part of my node.js app, and when I want to add the user to firebase if they are not set I get "Can't set headers after they are set. Here is my code: if (snapshot.hasChildren()) { console.log('User exists'); res.json(true); }else if(!snapshot.hasChildren()){ console.log('User does not exist'); firebaseRef.push(req.body); res.json(false); } When I look at the console logs I see logged "user does not exist" and the next line right after I see "user exists" any clue? – Chipe Jul 13 '15 at 22:32
  • Here is the full function I am doing in my server.js: firebaseRef.orderByChild('username').equalTo(user).on('value', function(snapshot) { if (snapshot.hasChildren()) { console.log('User exists'); res.json(true); }else if(!snapshot.hasChildren()){ console.log('User does not exist'); firebaseRef.push(req.body); res.json(false); } }); Its like the firebaseRef.push is calling the firebaseRef.orderByChild again – Chipe Jul 13 '15 at 22:34
  • Please don't post significant code in comments. Instead click the *edit* link under your question and add it there next time. – Frank van Puffelen Jul 13 '15 at 22:39
  • The `on` callback will remain registered, so as soon as you create the user it will fire again. You may want a `once` callback in this case. – Frank van Puffelen Jul 13 '15 at 22:43
  • 2
    That `once` is the answer to your last question. But note that we've now gone from "how can I check of a user exists?" to "how can I ensure that a callback runs only once?". StackOverflow really appreciates a "one question per question" approach. – Frank van Puffelen Jul 13 '15 at 22:47
  • Gotcha, I will keep that in mind for the future. Thanks for the help anyways! – Chipe Jul 13 '15 at 22:55
  • You're welcome. I realized straight away this had been asked before, which is why I only commented. I might still mark as a duplicate once I find a dupe. – Frank van Puffelen Jul 13 '15 at 22:58

0 Answers0