首页 > 解决方案 > 如何检索包含特定字符串的特定对象

问题描述

我有一个包含一系列 JSON 对象(练习)的数据库,我想只过滤和检索那些具有特定字符串的对象。

我下面的方法检索所有这些,我提供了特定的字符串(pathExercice)作为参数,但我不确定如何使用它来仅选择包含它的对象。

这是我目前在我的数据库中拥有的数据示例。在这种情况下,如果我的方法中提供的 pathExercice 与我的对象中的字符串匹配,我想检索这个对象

有什么线索吗?谢谢你

-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);
    });
}

标签: javascriptfirebasefirebase-realtime-database

解决方案


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);
    });

}


推荐阅读