Dawson
Dawson

Reputation: 23

Test if a username is in the User class of Parse in cloud code

On our website I want people to be able to report if a video is uploaded on Youtube so we have 3 fields on our website. A Username, password and video Id field. When the user clicks the Html button we have it call a javascript function.

    function reportUpload() {
    Parse.Cloud.run('report_upload',
    {
        username: "testUser",
        password: "testPassword",
        videoId: "VQv9xfOfLOY"
    },{
        success: function(result) {
            //Do Neat Stuff
        },
        error: function(e) {
            //error
        }
    });
}

That then calls our cloud code function. We need the cloud code function to pull the usernames from the User class and test if any of them match. If any do match they will then need to check if the passwords match. if all of that doesn't fail it will send a push notification to one of the users of the app.

So far in the cloud code funtion I have figured out how to query all the usernames.

Parse.Cloud.define("report_upload", function(request, response) {
    console.log(request.params);
    var query = new Parse.Query(Parse.User);
    query.find({
        success: function(results){
            var users = [];
            //extract out user names from results
            for(var i = 0; i < results.length; ++i){
                users.push(results[i].get("username"));
            }
            response.success(users);
            console.log(JSON.stringify(users));
        }, error: function(error){
            response.error("Error");
          }
    });
});

Thanks

Upvotes: 2

Views: 528

Answers (1)

Akshay Arora
Akshay Arora

Reputation: 1945

Seems like you want to check if a particular user has made an account already with a particular username.

You will have to query the User class. The user class is secured by default. In the cloud code, you should write Parse.useMasterKey(); to have a full access (Careful, read note below).

var myquery = new Parse.Query(Parse.User);
myquery.equalTo("username",yourUsernameHere);
myquery.find({
success: function(results){
if(results.length === 0)
{
    // User not found
}
else
{
    // found
}
});

Note: Using the master key overrides all individual access privileges for your data. Be absolutely sure what you are doing.

Upvotes: 1

Related Questions