Find Friends with Firebase - Web App

I'm new to Firebase and I'm having trouble implementing a search function for my application. I want users to be able to search other users with search.

If users were always looking for one word at a time, I wouldn't have a problem with that. But users can search by first name, last name, or first name. Does this mean I need to make three queries to the db?

My DB Structure Image

var myArray = [];
    $('#navSearchBtn').click(function () {

        // get search input values
        var searchQuery = $('#srch-term').val();
        var searchQuerySplit = searchQuery.split(" ");
        var firstName = searchQuerySplit[0];
        var lastName = searchQuerySplit[searchQuerySplit.length - 1];

        if (searchQuery) {

            // get rootRef
            var rootRef = firebase.database().ref();

            // If a user doesn't enter a full name, the query only
            // returns first names that match, I need to also return
            // last names that match the query.
            // Does this mean I repeat this query 3 other times (first name
            // , last name, and full name)?

            rootRef.child('users').orderByChild('displayName').startAt(searchQuery).endAt(searchQuery + '\uf8ff').on("child_added", function (snapshot) {
                var snapshotResult = snapshot.val();

                myArray.push(snapshotResult.displayName);

            })
            console.log(myArray);

        } else {
            console.log("nothing found");
        }
    })

      

+3


source to share





All Articles