需要遍历firebase数据

时间:2016-12-28 17:30:36

标签: javascript node.js

我试图在firebase中检索一些数据,但我无法弄清楚如何从收据树中检索每个expire_date值

enter image description here

在last_receipt_info中可能有0-N数量的“LFutx9mQbTTyRo4A9Re5I4cKN4q2”类型的收据和N个数量

enter image description here

我的代码是:

ref.child("recipts").once("value").then(function(questionsSnapshot) {
  return questionsSnapshot.forEach(function(questionSnapshot) {
    return console.log(questionSnapshot.val());
  });
});

这实际上为我带来了所有收件人的数据,但我不知道如何完全循环expire_date值。

我仔细地尝试了这一点,但没有带来数据:

  ref.child("recipts").once("value").then(function(a) {
      a.forEach(function(a){
      a.child("latest_receipt_info").once("value").then(function(b) {
          return console.log(b.val());  //Here for every subscription I wanted to make a function
      });
    });
  });

想法?谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用for ... in循环:

for (var key in questionSnapshot.val()) {
    // => do what you need
}

我从未使用过firebase,但我认为这样可以满足您的需求。

编辑:

如果我理解这一点,上面每次迭代中的key都会像LFutx9mQbTTyRo4A9Re5I4cKN4q2那样?如果是这样,您可以继续嵌套for...in循环:

for (var key in questionSnapshot.val()) {
    // key = LFutx9mQbTTyRo4A9Re5I4cKN4q2? something like that?

    for (var key2 in questionSnapshot.val()[key]) {
        // key2 = K_23AEu3cjcIKT1tTHf ???
        for (var item of questionSnapshot.val()[key][key2]['latest_receipt_info']) {
            console.log(item.expires_date);
        }
     }
 }

编辑2:

for (var key in questionSnapshot.val()) {
    for (var item of questionSnapshot.val()[key]['latest_receipt_info']) {
        console.log(item.expires_date);
    }
}

答案 1 :(得分:-1)

经过一番努力。我想通了

// we define available font weight and styles for each font here
const font = {
  OpenSans: {
    weights: {
      ExtraBold: '800',
      Bold: '700',
      SemiBold: '600',
      Light: '300',
      Normal: '400'
    },
    styles: {
      Italic: 'italic'
    }
  },
  Verdana: ...,
  Tahoma: ...,
  ...
}

// generate styles for a font with given weight and style
export const fontMaker = (options = {}) => {
  let { weight, style, family } = Object.assign({
    weight: null,
    style: null,
    family: 'OpenSans'
  }, options)

  const { weights, styles } = font[family]

  if (Platform.OS === 'android') {
    weight = weights[weight] ? weight : ''
    style = styles[style] ? style : ''

    const suffix = weight + style

    return {
      fontFamily: family + (suffix.length ? `-${suffix}` : '')
    }
  } else {
    weight = weights[weight] || weights.Normal
    style = styles[style] || 'normal'

    return {
      fontFamily: family,
      fontWeight: weight,
      fontStyle: style
    }
  }
}