【问题标题】:Is Firebase Database searchable using objects instead of references, with AngularFire $firebaseArray $keyAt(recordOrIndex)?使用 AngularFire $firebaseArray $keyAt(recordOrIndex) 可以使用对象而不是引用来搜索 Firebase 数据库吗?
【发布时间】: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


    【解决方案1】:

    答案似乎是否定的,您无法使用 AngularFire 搜索 Firebase 数据库。 (也许 AngularFire 2 有搜索,我没看。)我所做的是使用“普通香草”Firebase:

    var users = firebase.database().ref('users');
    users.orderByChild('email').equalTo(authData.user.email).once('value').then(function(snapshot) {
                console.log(snapshot.val());
    });
    

    第一行设置 Firebase ref 和之前的类似,除了我直接进入 users 数组,而不是使用 $FirebaseArray 进入 users 数组。

    第二行是完全不同的语法。首先,您必须指定您希望返回对象的顺序。是的,它返回一个对象,而不是一个数组。我试过snapshot.val().length(),发现它不是一个数组。 orderByChild('email') 所做的是访问'users' 数组中对象的'email' 属性。

    接下来我们进行查询。 equalTo(authData.user.email) 仅返回来自$FirebaseAuth 的电子邮件地址等于我们的'users' 数组中的电子邮件地址的对象。

    接下来,once('value') 创建一个 Promise 并等待异步数据。我尝试使用on(),但由于参数太多或其他原因无法使其正常工作。 once() 需要一个参数,可以是value、child_added、child_changed、child_removed 或child_moved。 value 参数用于在不更改子节点的情况下从某个位置获取数据。

    然后我们可以设置我们的then 承诺履行。您可以将返回的数据称为任何内容。这里叫snapshot。

    最后,snapshot.val() 提供数据库中的数据,看起来就像在 Firebase 控制台中一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 2016-08-06
      • 2020-09-23
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多