tinthetub
tinthetub

Reputation: 2196

Parse, Fetch and Display Current User

I'm attempting to fetch the currentuser's username, and have it displayed on screen, here is my code:

Parse.User.logIn(username, password, {
  success: function(user) {
    var currentUser = Parse.User.current(); 
    currentUser.fetch().then(function(fetchedUser) {
      var name = fetchedUser.getUsername();
    });
    $('.alertmsg').html('<div id="alert"><p>You are' + name + '</div>');
  }
});

Everything is working fine, but the username won't display.

Hope that this is an easy fix!

Upvotes: 0

Views: 3365

Answers (1)

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31590

You should move the line at which you update the html of alertmsg in the callback, that is because name is scoped to the callback function and does not exist outside of it:

Parse.User.logIn(username, password, {
  success: function(user) {
    var currentUser = Parse.User.current(); 
    currentUser.fetch().then(function(fetchedUser) {
      var name = fetchedUser.getUsername();
      $('.alertmsg').html('<div id="alert"><p>You are' + name + '</div>');
    });
  }
});

Upvotes: 5

Related Questions