gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:50:29 24 4
gpt4 key购买 nike

如何从 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

最佳答案

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

  • 你有一个用户登录
  • 你的请求记录的key是当前登录用户的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
});

关于typescript - 如何从 firebase 获取当前用户地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45477435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com