How to get the key from a Firebase data snapshot?

前端 未结 5 1217
南旧
南旧 2021-02-01 02:52

I\'m able to query my users array with an e-mail address and return the user\'s account info:

users.orderByChild(\'email\').equalTo(authData.user.em         


        
5条回答
  •  抹茶落季
    2021-02-01 03:21

    Realtime database:

    For this you can simple use: snapshot.key

    snapshot = firebase.database.DataSnapshot

    this.app.database()
            .ref('/data/')
            .on('value', function(snapshot) {
                const id = snapshot.key;
    
                //----------OR----------//
                const data = snapshot.val() || null;
                if (data) {
                  const id = Object.keys(data)[0];
                }
            });
    

    Firestore:

    snapshot.id

    snapshot = firebase.firestore.DocumentSnapshot

    this.app.firestore()
            .collection('collection')
            .doc('document')
            .onSnapshot(function(snapshot) {
                const id = snapshot.id;
    
                //----------OR----------//
                const data = snapshot.data() || null;
                if (data) {
                  const id = Object.keys(data)[0];
                }
            });
    

提交回复
热议问题