0

Heres's what I'm trying to do:

  1. Get an array named RecentPlayers containing 4 objectId's - This is working.

  2. Run a query for each of those objectId's to find their corresponding "name" (string). - Not Working.

  3. Put these names into a new array, unimaginatively named names. - This should work once #2 works.

The Code:

$(document).ready(function _RP(){
//Our objectId's array
var recentlyPlayed = Parse.User.current().get("RecentPlayers");
//Our names array
var names = [];
//Converting
var User = Parse.Object.extend("User");
var query = new Parse.Query(User);
for (var j = 0; j < recentlyPlayed.length; j++) {
        query.equalTo("objectId", recentlyPlayed[j]);
        query.find({
            success: function(results) {
                var name = results[j].get("name");
                    console.log(name);
                names.push(name);
            },
            error: function(error) {
                alert("Error: " + error.code + " " + error.message);
            }
        });
    }
names = names.sort();

The error:

var name = results[j].get("name");

Console:

(4) Uncaught TypeError: Cannot read property 'get' of undefined
query.find.succes
(anonymous function)
e 
(anonymous function)
x.each.x.forEach
c.extend.resolve
e
(anonymous function)
x.each.x.forEach
c.extend.resolve
e
(anonymous function)
x.each.x.forEach
c.extend.resolve
(anonymous function) 
e
(anonymous function)
g
c.extend.then
e
(anonymous function)
x.each.x.forEach
c.extend.resolve
j.onreadystatechange

I'm guessing this has to do with Parse.com's asynchronous nature. Thanks for your help!

2 Answers 2

1

In this case, query.containedIn might be a more suitable constraint. Try the following code snippet:

var query = new Parse.Query(Parse.User);
query.containedIn("objectId", recentlyPlayed);
query.find().then(function(users) {
   for(var i=0; i < users.length; i++) {
       names.push(users[i].get("name"));
   }
    names.sort();
});
Sign up to request clarification or add additional context in comments.

1 Comment

That handled the error message, and the function appears to be working properly now, except there are no results returned.
0

Thanks, eth3lbert, just needed to set i=0 and users[i] in name.push.

var query = new Parse.Query(User);
        query.containedIn("objectId", recentlyPlayed);
        query.find().then(function(users) {
            for (var i=0; i < users.length; i++) {
                names.push(users[i].get("name"));
            }   
names = names.sort();

Comments

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.