如何使用 angularfire2 检查 Firebase 中第 3 层的值?

How to check the value in 3rd layer in Firebase with angularfire2?

我可以检查值直到第二层,当它到达对象数组所在的第三层时我不再可以我想打印键的值 "nomeOficina"

Print db

Print log of snapshot.value()

要访问嵌套值 nomeOficina,您需要枚举 Oficinas 和每个 childOfOficinas.

下面的解决方案假定您只想在控制台中打印 nomeOficina 的每个值(但要使用此数据,您可能希望将整个对象推入一个数组以将其用于您的查看)。

我建议使用 Firebase forEach(查看 Firebase Docs):

let ref = firebase.database().ref('Oficinas');
    ref.once('value') // get the parent 'oficinas'
      .then((oficinas: DataSnapshot) => {
        oficinas.forEach((childOfOficinas: DataSnapshot) => { // use forEach to enumerate the children of oficinas
          childOfOficinas.forEach((child: DataSnapshot) => { // use forEach to enumerate each child of oficinas
            console.log(child.val().nomeOficina); // print the value nomeOficina
            return false; // you must return a boolean when you use forEach or the TypeScript compiler will throw an error, see Firebase docs for more on this
          });
          return false;
        });
      });

请注意,如果您使用的是 TypeScript,最好养成将类型添加到 DataSnapshot 值的习惯。