【发布时间】:2017-04-17 18:49:32
【问题描述】:
用户使用$firebaseAuth 登录后,Google 会发送用户的显示名称、电子邮件和照片网址。然后我想在我的 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? ));
【问题讨论】:
标签: firebase firebase-realtime-database angularfire