Parse.com:用户未定义(Parse.com: user is not defined)

我正在使用parse.com作为网络应用的后端。 我有一个用户已登录该应用,当用户登录该网站时,我希望他们的名字出现在网站的每个页面上。

以下是我尝试识别用户用户名的代码:

// user logs on and is redirected to this page var user = Parse.User.current(); user.on('change', function () { console.log(user.get("username")); }); user.fetch();

但是,当我检查控制台时,错误

Uncaught ReferenceError: user is not defined

存在。

在我的研究中,我修改了Unable中的代码以获取用户的数据 。 但是,在我的情况下,此代码不起作用。

任何帮助是极大的赞赏!

I'm using parse.com as a back end for a web app. I have a user that I have signed in to the app, and when the user is signed in to the website, I want their name to appear on every page in the website.

Here is my code for trying to recognize the username of the user:

// user logs on and is redirected to this page var user = Parse.User.current(); user.on('change', function () { console.log(user.get("username")); }); user.fetch();

However, when I check the console, the error

Uncaught ReferenceError: user is not defined

is present.

In my research, I've modified the code from Unable to get User's data. However, in my case, this code is not working.

Any help is greatly appreciated!

最满意答案

尝试这个:

var user = Parse.User.current(); user.fetch().then(function(fetchedUser){ var name = fetchedUser.getUsername(); }, function(error){ //Handle the error });

Try this:

var user = Parse.User.current(); user.fetch().then(function(fetchedUser){ var name = fetchedUser.getUsername(); }, function(error){ //Handle the error });Parse.com:用户未定义(Parse.com: user is not defined)

我正在使用parse.com作为网络应用的后端。 我有一个用户已登录该应用,当用户登录该网站时,我希望他们的名字出现在网站的每个页面上。

以下是我尝试识别用户用户名的代码:

// user logs on and is redirected to this page var user = Parse.User.current(); user.on('change', function () { console.log(user.get("username")); }); user.fetch();

但是,当我检查控制台时,错误

Uncaught ReferenceError: user is not defined

存在。

在我的研究中,我修改了Unable中的代码以获取用户的数据 。 但是,在我的情况下,此代码不起作用。

任何帮助是极大的赞赏!

I'm using parse.com as a back end for a web app. I have a user that I have signed in to the app, and when the user is signed in to the website, I want their name to appear on every page in the website.

Here is my code for trying to recognize the username of the user:

// user logs on and is redirected to this page var user = Parse.User.current(); user.on('change', function () { console.log(user.get("username")); }); user.fetch();

However, when I check the console, the error

Uncaught ReferenceError: user is not defined

is present.

In my research, I've modified the code from Unable to get User's data. However, in my case, this code is not working.

Any help is greatly appreciated!

最满意答案

尝试这个:

var user = Parse.User.current(); user.fetch().then(function(fetchedUser){ var name = fetchedUser.getUsername(); }, function(error){ //Handle the error });

Try this:

var user = Parse.User.current(); user.fetch().then(function(fetchedUser){ var name = fetchedUser.getUsername(); }, function(error){ //Handle the error });