调用firebase.auth()。onAuthStateChanged()时出现“ Uncaught TypeError:无法读取未定义的属性'resolve'”

时间:2019-09-04 14:16:19

标签: javascript firebase firebase-authentication

当我尝试创建Auth状态观察器时,遇到以下错误消息。我已将功能降到最低(请参见代码片段),但错误仍然存​​在。

1: firebase.auth().onAuthStateChanged(function(user) {
2:     if (user) {
3:       // User is signed in.
4:       console.log(user)
5:     } else {
6:       // No user is signed in.
7:       console.log("no user")
8:     }
9:   });

错误消息:

Uncaught TypeError: Cannot read property 'resolve' of undefined
    at X.h.onAuthStateChanged (firebase-auth.js:194)
    at X.a [as onAuthStateChanged] (firebase-auth.js:142)
    at existingUserLogin (index.min.js:2)
    at (index):150

(index):150是函数existingUserLogin()

的位置

(index.min.js:2)是指所包含代码段中的line 1

onAuthStateChanged()方法包含以下内容:

fireauth.Auth.prototype.onAuthStateChanged = function(
    nextOrObserver, opt_error, opt_completed) {
  var self = this;
  // State already determined. Trigger immediately, otherwise initState will
  // take care of notifying all pending listeners on initialization.
  // In this case we do not trigger synchronously and trigger via a resolved
  // promise as required by specs.
  if (this.isStateResolved_) {
    // The observer cannot be called synchronously. We're using the
    // native Promise implementation as otherwise it creates weird behavior
    // where the order of promises resolution would not be as expected.
    // It is due to the fact fireauth and firebase.app use their own
    // and different promises library and this leads to calls resolutions order
    // being different from the promises registration order.
    Promise.resolve().then(function() {
      // This ensures that the first time notifyAuthListeners_ is triggered,
      // it has the correct UID before triggering the user state change
      // listeners.
      self.userStateChangeUid_ = self.getUid();
      if (goog.isFunction(nextOrObserver)) {
        nextOrObserver(self.currentUser_());
      } else if (goog.isFunction(nextOrObserver['next'])) {
        nextOrObserver['next'](self.currentUser_());
      }
    });
  }
  return this.onUserStateChanged_(
      /** @type {!firebase.Observer|function(*)|undefined} */ (nextOrObserver),
      /** @type {function(!Error)|undefined} */ (opt_error),
      opt_completed);
};

1 个答案:

答案 0 :(得分:1)

如上所述,解决方案是确保使用正确/匹配的firebase,firebase-firestore和firebase-auth版本。似乎是由于早期版本的firebase-auth在firebase上使用了Promise属性,而较新版本的firebase不再具有Promise属性引起的。