ian
ian

Reputation: 1002

Retrieving class from parse.com with cloud code

Im having trouble with the my cloud code on parse.com. I have a class on parse.com in my data browser called "User". In this class I have seven users. each user has the keys "objectId" and "username". I want to import an array of the "objectId's" in the class "User", but I cannot retrieve them. It keeps saying "User" is not defined in my cloud code log. Here is my code:

Parse.Cloud.define("getUsers", function(request, response) {
  var query = new Parse.Query(User);
  var names = query.get("username");
  console.log("The users are" + names);

});

Im new to javascript so i would really appreciate some help. Thanks!

Upvotes: 0

Views: 155

Answers (1)

Dehli
Dehli

Reputation: 5960

Your problem is because User is a Parse class. The code should be:

var query = new Parse.Query(Parse.User);

Additionally, most of your function needs rework.

Parse.Cloud.define("getUsers", function(request, response) {
    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);
        }, error: function(error){
            response.error("Error");
        }
    });
});

Upvotes: 2

Related Questions