Note that there are some explanatory texts on larger screens.

plurals
  1. POError when storing variables for use in later functions
    primarykey
    data
    text
    <p>I'm trying to set up user authentication with Firebase and Twitter. The code from <a href="https://stackoverflow.com/a/905324/1683992">this answer</a> works to a point:</p> <pre><code>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; } }; })(); </code></pre> <p>I can access the auth variable using <code>dataStore.getAuth()</code> and user.id is logged to the console. Calling <code>dataStore.getAuth().login('twitter')</code> also works, and I'm redirected to the twitter login page.</p> <p>But if I try to return the user id externally using <code>dataStore.getUser().id</code> I get a <code>cannot read property 'id' of undefined</code> error.</p> <p>I think I'm probably just returning <code>user</code> in the wrong way, so if anyone could shed any light on the issue I'd very much appreciate it.</p> <p><strong>UPDATE:</strong> Thanks to the answers from @BenjaminWarren and @SpinyNorman I've updated the code to use the correct variables as below:</p> <pre><code>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); }); </code></pre> <p>Console output looks like this:</p> <pre><code>Uncaught TypeError: Cannot read property 'id' of undefined main.js:37 User ID: 222936694, Provider: twitter main.js:14 </code></pre> <p>So <code>dataStore.getUser().id</code> is being logged before the auth object has been returned, and therefore is undefined. I've tried different setTimout values etc on the getUser() return but it's alsways returned before the <code>auth</code></p> <p>Cheers,</p> <p>James</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload