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

时间:2017-08-03 07:20:42

标签: 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 = 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'
            }
         }) 


            }
        });

    });     
  }

我的构造函数,我声明currentUserId。

constructor(public navCtrl: NavController, public navParams: NavParams, private angFire: AngularFireDatabase,private af: AngularFireDatabase,
  private afAuth: AngularFireAuth) {
    //this.request = angFire.list('/request');

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

        var user = firebase.auth().currentUser;
        var currentUserId = user.uid;

    //this.userreq = angFire.list(`${this.userkey}`);
    //this.reqdetails = angFire.list('reqdetails');
    //this.request = angFire.list(this.data);

  }

这就是我的firebase控制台的样子 enter image description here

现在我得到了这个错误

enter image description here

1 个答案:

答案 0 :(得分:1)

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

  • 您已登录用户
  • 您的请求记录的密钥是当前登录用户的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为您的openMapPage()功能提供的两种可能性:

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;
  });

2)AngularFire2:

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
});