Firebase v3 Query by Grandchild

后端 未结 2 990
时光取名叫无心
时光取名叫无心 2020-12-18 13:56

When registering new email/password type users, I need to make user that the displayName that they enter does not already exist in my Realtime Database before

2条回答
  •  执念已碎
    2020-12-18 14:29

    Just thought I'd share my somewhat fleshed out solution. Call with myApp.displayNameExists('Joe').

    var myApp = (function() {
        var pub = {};
        pub.displayNameExists = function(name) {
                var users = firebase.database().ref('users');
                var duplicate = users.orderByChild('displayName').equalTo(name);
                duplicate.once('value').then(function(snap) {
                    if (snap.val()) {
                        console.log('found. ask for new display name');
                    } else {
                        console.log('name unique.  ok to write this user to db');
                    }
                }, function(error) {
                    // The Promise was rejected.
                    console.error(error);
                });
            }
            //API
        return pub;
    }());
    

提交回复
热议问题