I have a database that contains a series of the JSON object (exercises), I would like to filter and retrieve only those that have a specific string.

my method below retrieve all of them, I provide the specific string (pathExercice) as a parameter, but I'm not sure how to use to select only the objects that contains it.

here is an example of data that I currently have on my database. in this case, I would like to retrieve this object if my pathExercice provided in my method, matches the string inside my object

Any clues? thank you

-Le7BeKeG9Zes6mZ7gUm
      date: 1557063173016
      pathExercice: "DeveloppeCouche"
      reps: 10
      weight: 10
export const readUserData = async (pathExercice) => {
    let userID = firebase.auth().currentUser.uid
    let ref = firebase.database().ref('users/' + userID + '/exercices')
    return ref.once("value").then(snapshot => {
        console.log(snapshot.val());
        return snapshot.val()
    }).catch(err => {
        console.log('error:  ' + err);
    });
}

3 Answers

2
Ulysses88 On Best Solutions

found the solution, thx to those who have taken the time to read my post.

export const readUserData = async (pathExercice) => {
let userID = firebase.auth().currentUser.uid
let ref = firebase.database().ref('users/' + userID + '/exercices')
return ref.orderByChild('pathExercice').equalTo(pathExercice).once("value")
    .then(snapshot => {
        console.log(snapshot.val());
        return snapshot.val()
    }).catch(err => {
        console.log('error:  ' + err);
    });

}

1
Frank van Puffelen On

The Firebase Database does not support wildcard string match queries, so you can't use the database to search for pathExercice values that contain a certain substring.

You can search for pathExercice that start with a certain substring, like this:

let ref = firebase.database().ref('users/' + userID + '/exercices')
return ref.orderByChild('pathExercice').startAt('Devel').endAt('Devel\uF7FF').once("value").then(snapshot => {

If you want to filter on pathExercice values that contain a certain substring, you will have to do that in your code. Note this means you'll be reading all child nodes from the database, so be mindful of how many child nodes there are.

The code should look something like this:

export const readUserData = async (pathExercice) => {
    let userID = firebase.auth().currentUser.uid
    let ref = firebase.database().ref('users/' + userID + '/exercices')
    return ref.once("value").then(snapshot => {
        results = [];
        snapshot.forEach(exercise => {
            if (exercise.child('pathExercice').val().indexOf('Couche') >= 0) {
                results.push(exercise.val());
            }
        }
        return results;
    }).catch(err => {
        console.log('error:  ' + err);
    });
}
0
Community On

Try this, it would obtain the string inside pathExercise

export const readUserData = async (pathExercice) => {
    let userID = firebase.auth().currentUser.uid
    let ref = firebase.database().ref('users/' + userID + '/exercices')
    return ref.once("value").then(snapshot => {
        console.log(snapshot.val());
        if (snapshot.val().pathExercice === "DeveloppeCouche") {
             //some action here
        }
        return snapshot.val()
    }).catch(err => {
        console.log('error:  ' + err);
    });
}

Hope this answers your question.