我试图在firebase中创建独特的用户名,我觉得我已经关闭但是我已经被卡住了。这就是我正在尝试的,但我永远无法让变量在快照功能之外工作。我想要做的就是查看是否有一个用户节点具有相同的用户名(这是有效的),但我无法在该功能之外获取快照变量,如果有的话,我无法阻止用户创建找到一个节点。谢谢
signUp() {
let loader = this.loadingController.create({
content: "Please wait"
});
loader.present();
var userName = this.account.username;
var testCount = firebase.database().ref('users/' + userName + '/userName');
let testAlert: string;
testCount.on('value', function (snapshot) {
alert(snapshot.val());
testAlert = snapshot.val();
});
alert(testAlert);
this.angularFireAuth.auth.createUserWithEmailAndPassword(
this.account.email,
this.account.password
).then((value) => {
loader.dismiss();
this.login();
}).catch((error) => {
loader.dismiss();
let errorMessage = error;
if (errorMessage && errorMessage.message) {
let message = errorMessage.message.replace(/<(?:.|\n)*?>/gm, '');
let toast = this.toastController.create({
message: message,
duration: 6000,
position: 'bottom'
});
toast.present();
}
});
}