简体   繁体   中英

Get custom attribute from User class on Parse.com [JavaScript]

I'm working on a website with Parse.com and JavaScript. I have my "User" class, but I have added more fields than those that come by default.

I want to get one specific attribute from my User class (IdGroup) when user logs in, so I'm trying to capture the IdGroup using user object when login has been succesfully.

I have tried this:

Parse.User.logIn(username, pass, {
    success: function (user) {

        //var idGroup = user.get("IdGroup");

        localStorage.setItem("parseUser", true);
        localStorage.setItem("guestUser", false);
        localStorage.setItem("username", username);

        user.fetch().then(function (fetchedUser) {
            var idGroup = user.get("IdGroup");
            localStorage.setItem("IdGroup", idGroup);
        });

        window.location.href = 'index.html';
    },
    error: function (user, error) {
        alert('Invalid username or password!');
    }
});

This doesn't work.

If anyone could give me a hand, I will really appreciate.

Thanks!!

I would have assumed that the user was already fetched at login.

But your callback function passes in the object fetchedUser, and you're accessing the user object. So, var idGroup = fetchedUser.get("IdGroup"); is probably what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM