Firebase数据库是否可以使用AngularFire$firebaseArray$keyAt(recordOrIndex)使用对象而不是引用进行搜索?
用户使用Firebase数据库是否可以使用AngularFire$firebaseArray$keyAt(recordOrIndex)使用对象而不是引用进行搜索?,firebase,firebase-realtime-database,angularfire,Firebase,Firebase Realtime Database,Angularfire,用户使用$firebaseAuth登录后,谷歌会发送用户的显示名、电子邮件和照片URL。然后我想在Firebase数据库中查找用户的帐户。我不能使用$getRecord(key),因为谷歌没有告诉我用户的密钥。似乎我应该使用$keyAt(recordOrIndex),然后使用$getRecord(key)$keyAt(recordOrIndex)可以很好地使用索引$keyAt(recordOrIndex)可以很好地处理我用$getRecord(key)检索到的记录。我无法让$keyAt(reco
$firebaseAuth
登录后,谷歌会发送用户的显示名、电子邮件和照片URL。然后我想在Firebase数据库中查找用户的帐户。我不能使用$getRecord(key)
,因为谷歌没有告诉我用户的密钥。似乎我应该使用$keyAt(recordOrIndex)
,然后使用$getRecord(key)
$keyAt(recordOrIndex)
可以很好地使用索引$keyAt(recordOrIndex)
可以很好地处理我用$getRecord(key)
检索到的记录。我无法让$keyAt(recordOrIndex)
处理我从Google使用$firebaseAuth
返回的用户数据创建的对象
我尝试了完整的对象(displayName、email、photoURL)和只包含电子邮件地址的对象。后者是我更愿意使用的。两者都不起作用
app.controller('LoginModalInstanceCtrl', ['$scope', '$location', '$uibModalInstance', '$firebaseArray', '$firebaseObject', '$firebaseAuth', function($scope, $location, $uibModalInstance, $firebaseArray, $firebaseObject, $firebaseAuth) {
// Create Firebase3 reference
var ref = firebase.database().ref();
// Set up Firebase Auth
$scope.authObj = $firebaseAuth();
var authData = $scope.authObj.$getAuth();
$scope.authData = authData;
// Google OAuth login handler
$scope.loginGoogle = function() {
$scope.authData = null;
$scope.error = null;
$scope.authObj.$signInWithPopup("google")
.then(function(authData) {
$scope.authData = authData;
console.log(authData);
console.log("Your displayName is:", authData.user.displayName);
console.log("Your email is:", authData.user.email);
console.log("Your photoURL is:", authData.user.photoURL);
var record = {
displayName: authData.user.displayName,
email: authData.user.email,
photoURL: authData.user.photoURL
};
var emailObject = {
email: authData.user.email
};
// look up account
var users = $firebaseArray(ref.child('users'));
users.$loaded()
.then(function() {
console.log("Array loaded!");
var key1 = users.$keyAt(1);
console.log(key1); // -Khi6OxAo339ye6xoG3i
var record = users.$getRecord(key1);
console.log(record); // Object with displayName, email, and photoURL
var key1 = users.$keyAt(record);
console.log(key1); // -Khi6OxAo339ye6xoG3i
var objectKey = users.$keyAt(object);
console.log(objectKey); // null
var emailKey = users.$keyAt(emailObject);
console.log(emailKey); // null
});
$uibModalInstance.close(); // close modal window
$location.path('/languagetwo/'); // return to the homepage
}).catch(function(error) {
console.error("Authentication failed:", error);
});
};
我是否应该使用$firebaseObject
而不是$firebaseArray
:
var user = $firebaseObject(ref.child('users').child( SOMETHING HERE? ));
答案似乎是否定的,您无法使用AngularFire搜索Firebase数据库。(也许AngularFire 2有搜索功能,我没有看。)我所做的是使用“纯香草”火基:
var users = firebase.database().ref('users');
users.orderByChild('email').equalTo(authData.user.email).once('value').then(function(snapshot) {
console.log(snapshot.val());
});
第一行设置Firebaseref
,与之前类似,只是我直接进入users
数组,而不是使用$FirebaseArray
进入users
数组
第二行是完全不同的语法。首先,必须指定返回对象的顺序。是的,它返回一个对象,而不是数组。我尝试了snapshot.val().length(),发现它不是数组。orderByChild('email')
所做的是访问'users'
数组中对象的'email'
属性
接下来我们进行查询equalTo(authData.user.email)
仅返回$FirebaseAuth
中的电子邮件地址与'users'
数组中的电子邮件地址相等的对象
接下来,one('value')
创建一个承诺并等待异步数据。我尝试在()上使用,
,但无法使其工作,参数太多或其他原因once()
需要一个参数,该参数可以是值
,添加的子项
,更改的子项
,删除的子项
,或移动的子项
。value
参数用于在不更改子节点的情况下从某个位置获取数据
然后,我们可以设置我们的然后
承诺履行。您可以调用返回的数据。这里称之为快照
最后,snapshot.val()
提供数据库中的数据,看起来就像Firebase控制台中的数据一样