解析,获取和显示当前用户 [英] Parse, Fetch and Display Current User

查看:79
本文介绍了解析,获取和显示当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前用户的用户名,并将其显示在屏幕上,这是我的代码:

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!

推荐答案

您应该在回调中移动更新alertmsg的html的行,这是因为name的作用域是回调函数,并且在它之外不存在:

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>');
    });
  }
});

这篇关于解析,获取和显示当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆