Typescript 如何从firebase获取当前用户地址

Typescript 如何从firebase获取当前用户地址,typescript,firebase,ionic-framework,firebase-realtime-database,ionic2,Typescript,Firebase,Ionic Framework,Firebase Realtime Database,Ionic2,如何从firebase获取当前用户地址?这部分需要帮助。我已使用当前用户的呼叫地址,但无法获取该地址。 我能够从firebase获取所有用户地址。但当我在下面尝试检索当前用户地址时,它失败了 openMapPage() { var ref = firebase.database().ref("request"); ref.once("value").then((snapshot) => { // <------ Here! var a = s

如何从firebase获取当前用户地址?这部分需要帮助。我已使用当前用户的呼叫地址,但无法获取该地址。 我能够从firebase获取所有用户地址。但当我在下面尝试检索当前用户地址时,它失败了

 openMapPage()
  {


    var ref = firebase.database().ref("request");
    ref.once("value").then((snapshot) => { // <------ Here!
        var a = snapshot.exists();  // true
        var c = snapshot.hasChild("reqdetails"); // true
        var d = snapshot.child('reqdetails').exists();
        var requestsKey = snapshot.key;
        var requestsValue = snapshot.val();


        //var currentadd = requestsKey.reqdetails.address;


          //this.afAuth.authState.take(1).subscribe(data =>{
      ///this.profileData = this.af.object(this.user);
            //console.log(this.profileData);
    //}) 

    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
    // User is signed in.
         var user = firebase.auth().currentUser;
         var currentUserId = user.uid;

         var currentadd = this.currentUserId.regdetails.address;
          console.log("Current User Address");
          console.log(currentadd);

        } else {
    // No user is signed in.
        }
      });



        snapshot.forEach((childSnapshot) => { // <------ And here!
            var requestKey = childSnapshot.key;
            var requestValue = childSnapshot.val();
            var reqdetails = requestValue.reqdetails;
            var AllUserAddress = requestValue.regdetails.address;


           //var currentUserAdd = currentUserId.address;




            if (reqdetails) {
                this.data = requestKey;
                console.log(this.data);
                //this.arr.push(requestKey);
                //console.log(this.arr);

                 this.getRequest = this.angFire.list('request', {
                   query: {
                   orderByChild: 'reqdetails',
                   startAt: 'reqdetails'
            }
         }) 


            }
        });

    });     
  }
这就是我的firebase控制台的外观

现在我发现了这个错误


以下答案做出了以下假设:

  • 您有一个用户登录
  • 请求记录的密钥是当前登录用户的uid
您的问题是,在创建数据库引用时:

var ref = firebase.database().ref("request");
您没有使用当前用户的uid以
request
的子节点为目标。您需要获取当前用户的uid,然后通过以下方式创建数据库引用:

var uid = firebase.auth().currentUser.uid;
var ref = firebase.database().ref("request/" + uid);
您似乎认为firebase中的身份验证数据与实时数据库之间存在连接。Firebase中这两个东西之间没有任何联系-您只能使用用户的uid将实时数据库中的数据链接到用户

以下是使用Firebase SDK和AngularFire2实现
OpenMapage()
函数的两种可能性:

1)Firebase SDK:

var uid = firebase.auth().currentUser.uid; // Gets current user uid as string
var ref = firebase.database().ref('request/' + uid); // Make database ref which points to a child node of request that matches the current user's uid
ref.once('value', (request) => {
    var currentUserAddress = request.val().reqdetails.address;
  });
import 'rxjs/add/operator/switchMap';
var auth$ = this.afAuth.authState; // Get an observable of the authstate for the current user

var userRequest$ = auth$.switchMap((auth: firebase.User) => { // Use switchMap to subscribe and get the authstate
    return this.af.object('request/' + auth.uid); // Use the uid to get the appropriate "request" node
});

userRequest$.subscribe((request) => { // subscribe to the userRequest$ observable (I would suggest returning this to your component and using the async pipe instead of subscribing here)
  var currentAdd = request.reqdetails.address; // access the address
});
2)角火2:

var uid = firebase.auth().currentUser.uid; // Gets current user uid as string
var ref = firebase.database().ref('request/' + uid); // Make database ref which points to a child node of request that matches the current user's uid
ref.once('value', (request) => {
    var currentUserAddress = request.val().reqdetails.address;
  });
import 'rxjs/add/operator/switchMap';
var auth$ = this.afAuth.authState; // Get an observable of the authstate for the current user

var userRequest$ = auth$.switchMap((auth: firebase.User) => { // Use switchMap to subscribe and get the authstate
    return this.af.object('request/' + auth.uid); // Use the uid to get the appropriate "request" node
});

userRequest$.subscribe((request) => { // subscribe to the userRequest$ observable (I would suggest returning this to your component and using the async pipe instead of subscribing here)
  var currentAdd = request.reqdetails.address; // access the address
});

Ok reqdetails和regdetails有地址。但是我无法从当前用户处获取地址。您是否尝试了console.log(childsnapshot.val())以确保首先收到一个值?假设childsnapshot的值是您提供的屏幕截图中的“0AJ2wNW…”节点,我看不出您如何从快照中获取地址有任何问题。是的。我不明白为什么我不能获得当前的用户地址。我试图跟踪你给我的链接。现在它给了我一个未定义的currentUserId错误。我已经更新了我的帖子&39;s返回未定义,因为currentid只是当前用户的字符串值';是uid。currentid变量不是数据库中的对象,而是firebase.auth()中的字符串。当前用户今天晚些时候,当我在我的电脑前时,我会更改我的答案并填写更多的内容。谢谢你解释得很清楚。请你也帮我看看这个好吗?