问题描述
如何从Firebase获取当前用户地址?这部分需要帮助.我使用了当前用户的电话来拨打该地址,但无法获取该地址.我能够从Firebase获取所有用户地址.但是,当我在下面尝试检索当前用户地址时,它失败了.
How do I get the current user address from firebase? Need help for this part. I have used current user's to call the address, but can't get it.I am able to get all the user address from the firebase. But when I tried below to retrieve the current user address it failed.
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的构造函数.
My constructor where i declare 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控制台的外观
This is how my firebase console look like
现在我犯了这个错误
推荐答案
以下答案做出了这些假设:
The following answer makes these assumptions:
- 您有一个用户登录
- 您的请求记录的键是当前登录用户的uid
您的问题是创建数据库引用时:
Your problem is that when you create the database reference:
var ref = firebase.database().ref("request");
您没有使用当前用户的uid来定位 request
的子节点.您需要获取当前用户的uid,然后以这种方式创建数据库引用:
You are not targeting the child node of request
using the current user's uid. You need to get the current user's uid and then create the database reference this way:
var uid = firebase.auth().currentUser.uid;
var ref = firebase.database().ref("request/" + uid);
您似乎在想Firebase中的身份验证数据与实时数据库之间存在联系.Firebase中的这两件事之间没有联系-您只能使用用户的uid将实时数据库中的数据链接到用户.
You seem to be thinking that there is a connection between the authentication data in firebase and the realtime database. There is no connection between these two things in Firebase - you can only use the user's uid to link data in the realtime database to a user.
以下是将Firebase SDK和AngularFire2用于您的 openMapPage()
函数的两种可能性:
Here are two possibilities using the Firebase SDK and AngularFire2 for your openMapPage()
function:
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
});
这篇关于如何从Firebase获取当前用户地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!