存储变量以便在以后的函数中使用时出错

时间:2013-10-23 08:56:36

标签: javascript variables global-variables firebase

我正在尝试使用Firebase和Twitter设置用户身份验证。 this answer中的代码适用于某一点:

var url = 'https://molovo-comments.firebaseio.com/';
var myDataRef = new Firebase(url);

var dataStore = (function(){
  var user = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, data) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (user) {
      // user authenticated with Firebase
      user = data;          
      console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
    } else {
      // user is logged out
    }
  });

  return {
    getAuth : function() {
      if (auth) return auth;
        // else show some error that it isn't loaded yet;
    },
    getUser : function() {
      if (user) return user;
        // else show some error that it isn't loaded yet;
    }
  };
})();

我可以使用dataStore.getAuth()访问auth变量,user.id会记录到控制台。调用dataStore.getAuth().login('twitter')也有效,我将被重定向到Twitter登录页面。

但如果我尝试使用dataStore.getUser().id在外部返回用户ID,则会收到cannot read property 'id' of undefined错误。

我想我可能只是以错误的方式回归user,所以如果有人能对这个问题有所了解,我会非常感激。

更新 感谢@BenjaminWarren和@SpinyNorman的答案,我已经更新了代码,使用了如下正确的变量:

var dataStore = (function(){
  var userObj = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, user) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (user) {
      // user authenticated with Firebase
      userObj = user;
      console.log('User ID: ' + userObj.id + ', Provider: ' + userObj.provider);
    } else {
      // user is logged out
    }
  });

  return {
    getUser : function() {
      if (userObj) return userObj;
      // else show some error that it isn't loaded yet;
    },
    getAuth : function() {
      if (auth) return auth;
      // else show some error that it isn't loaded yet;
    }
  }
})();

jQuery(document).ready(function($) {
  console.log(dataStore.getUser().id);
});

控制台输出如下所示:

Uncaught TypeError: Cannot read property 'id' of undefined main.js:37
User ID: 222936694, Provider: twitter main.js:14

因此在返回auth对象之前会记录dataStore.getUser().id,因此未定义。我在getUser()返回时尝试了不同的setTimout值等,但它总是在auth

之前返回

干杯,

詹姆斯

2 个答案:

答案 0 :(得分:0)

试试这个:

var url = 'https://molovo-comments.firebaseio.com/';
var myDataRef = new Firebase(url);

var dataStore = (function(){
  var user = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, user) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (user) {
      // user authenticated with Firebase
      console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
    } else {
      // user is logged out
    }
  });

  return {
    getAuth : function() {
      if (auth) return auth;
        // else show some error that it isn't loaded yet;
    },
    getUser : function() {
      if (user) return user;
        // else show some error that it isn't loaded yet;
    }
  };
})();

查看其他一些参考文献,function(error, user)似乎是常见的格式。您的方式看起来else if (user)总是返回false,因为data正在传递而不是user

答案 1 :(得分:0)

在我看来,当前代码根本不会记录用户ID,因为user变量永远不会更新。它以null开头,仅在(...) if(user) { user = data }中设置,永远无法达到。我认为该行应该改为} else if (data) {

var url = 'https://molovo-comments.firebaseio.com/';
var myDataRef = new Firebase(url);

var dataStore = (function(){
  var user = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, data) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (data) {
      // user authenticated with Firebase
      user = data;          
      console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
    } else {
      // user is logged out
      console.log('Not logged in.');
      user = {id: null}; // for example.
    }
  });

  return {
    getAuth : function() {
      if (auth) return auth;
        // else show some error that it isn't loaded yet;
    },
    getUser : function() {
      if (user) return user;
        // else show some error that it isn't loaded yet;
    }
  };
})();
编辑:正如Benjamin Warren所指出的,这可能是由于函数参数的名称从user更改为data以避免与先前定义的user变量发生冲突而引起的。我假设在将参数名称从user更改为data后,日志功能也停止了。