2

Hey guys i am having a small issue with Parse for Javascript at the moment, I have used Parse before but for Android and for some reason found it more easier to understand than using it with Javascript.

My current issue is that I am trying to query the User object in my parse database to see whether or not they are an admin or not by checking to see in the "role" column has an integer of 1 or 2. 1 representing a Admin and 2 representing an ordinary user.

If I can remember correctly querying a User is different from querying a normal object in the database, So I am unsure why this query does not carry out correctly. If i take out the query the function works fine and displays the set message of "welcome" in an alert box but when I add in the query it doesnt seem to carry out the function.

My table in the database looks like this: enter image description here

My current code is as follows:

$('.form-signin').on('submit', function(e) {

    // Prevent Default Submit Event
    e.preventDefault();

    // Get data from the form and put them into variables
    var data = $(this).serializeArray(),
        username = data[0].value,
        password = data[1].value;

    // Call Parse Login function with those variables
    Parse.User.logIn(username, password, {
        // If the username and password matches
        var query = new Parse.Query(Parse.User);
        query.equalTo("role", 1); //finding the role number

        query.find({
            success: function(user) {
                alert('Welcome!');
            },
            // If there is an error
            error: function(user, error) {
                console.log(error);
            }
        });
    });
});

1 Answer 1

2

logIn() is fulfilled with the user that was logged in, so there's no need for an additional query to see that user's attributes. Parse.User.current is another way to get the current user at any time after a successful login.

Edit - This code logs in a user and checks that the user has role set to the number 1:

Parse.User.logIn(username, password).then(function(user) {
    var role = user.get("role");
    if (role == 1) {
        console.log("login success, role is 1");
    } else {
        console.log("login success, role is " + role);
    }
}, function(error) {
    // handle error
});
Sign up to request clarification or add additional context in comments.

5 Comments

Hi there thank you for the reply, i did notice that they had a specific class for Roles but as a beginner programmer i didnt know how to fully utilize that class so instead went a more simplistic route. Also about your code, how does it check whether the role number is equal to 1 to ensure that only Admins can log in.
@hero8110 - see the variable role? get() returns the value of that column. So you can test it if (role == 1) {}
Hey there i had a go at editing my code but i dont think i have implemented your logic right, im really sorry quite new to java script but this is what i did gyazo.com/cc6194ccca2a90fc27795be87e8851d2
You just want to login a user and check if the user has admin set to 1? Please see edit. Some of that code I had in the answer was to just illustrate a point (e.g. that you can call with promises or callbacks, or that User.current() works after login, etc.)
Hey there sorry i was completely oblivious to that, goes to show how new i am to java script and the jargon. I believe this works thank you sir for your time and effort, it is much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.