无法遍历firebase中的子记录

时间:2017-07-30 07:09:54

标签: firebase ionic-framework ionic2

您好我想弄清楚如何使用只有reqdetails的用户密钥遍历请求表。我试过跟随文档,但它没有用。 我只需要过滤掉只有reqdetails的所有用户密钥。例如,OAJ2WNWQPUfwJCpAJ11FWIA8kPn2的用户密钥具有reqdetails。

顺便说一句,我正在关注此链接: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot

这是我的firebase控制台:

enter image description here

这是我的声明和构造函数

import { ADD_TODO } from '../constants/actionTypes';

function getId(state) {
  return state.todos.reduce((maxId, todo) => {
    return Math.max(todo.id, maxId);
  }, -1) + 1;
}

const initialState = {
  todos: [{
    id: 0,
    text: 'Initial Todo',
    completed: false
  }]
};

export default function todos(state = initialState, action) {

  switch (action.type) {

    case ADD_TODO:
      return {
        ...state,
        todos: [{
          text: action.text,
          completed: false,
          id: getId(state)
        }, ...state.todos]
      };

    default:
      return state;
  }
}

这是我的OpenMapPage方法

  request: FirebaseListObservable<any>;
  userkey: FirebaseListObservable<any>;
  reqdetails: FirebaseListObservable<any>;
  userreq: FirebaseListObservable<any>;

  constructor(public navCtrl: NavController, public navParams: NavParams, angFire: AngularFireDatabase) {
    this.request = angFire.list('/request');
    this.userreq = angFire.list(`${this.userkey}`);
    this.reqdetails = angFire.list('reqdetails');
  }

1 个答案:

答案 0 :(得分:1)

您需要在所有请求中指定目标请求或循环的键。

如果您知道密钥:

ref.child(REQUEST_KEY).once("value", function(snapshot) {
    var requestKey = snapshot.key;
    var requestValue = snapshot.val();

    var reqdetails = requestValue.reqdetails;

    console.log(reqdetails);
}

如果您想要所有请求:

ref.once("value", function(snapshot) {
    var requestsKey = snapshot.key;
    var requestsValue = snapshot.val();

    snapshot.forEach(function(childSnapshot) {
        var requestKey = childSnapshot.key;
        var requestValue = childSnapshot.val();

        var reqdetails = requestValue.reqdetails;

        console.log(reqdetails);
    });
}