Firebase Database query list for not equal to

大城市里の小女人 提交于 2019-12-12 04:15:27

问题


I am using Firebase in my Angular 4 app using AngularFire2. I have my database structure something like this.

{
  "users": {
    "-Kr6HDbj9tMxmxsQ8UnM": {
      "name": "abc",
      "email": "xyz@example.com",
      "uid": "XLTPwGtFYcX6xVHbJGerHxGayT2"
    },
    "-Kr6JBzN_9PZAMctYwt-": {
      "name": "pqr",
      "email": "pqr@example.com"
      "uid": "cdIj8YOncYYiUIEJrb65ynfFcl2"
    },
    ............
  }
}

I want to retrieve the last two entries of this data structure but exclude the data where uid is not equal to auth.uid.


回答1:


I think it would be easier if you changed the datastructure so the key of each user is their uid, like this.

{
"users": {
    "XLTPwGtFYcX6xVHbJGerHxGayT2": {
      "...": ".........",
      "...": "........."
    },
    "cdIj8YOncYYiUIEJrb65ynfFcl2": {
      "...": ".........",
      "...": "........."
    }
}

Then you could easily access the specific users properties With the https://firebase.google.com/docs/reference/js/firebase.database.Reference#once

Example

var ref = firebase.database().ref("users/"+auth.uid);
ref.once('value')
    .then(function(dataSnapshot) {
    // handle read data.
});


来源:https://stackoverflow.com/questions/45593897/firebase-database-query-list-for-not-equal-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!