1

I'm new to JS and i had to use it for Cloud Code Parse feature. I have a class called "user_picture", through the code i query all the objects and go through it's "City" attribute. i want the response to be an array of unique city names. Anyway, here is the code i'm working on:

Parse.Cloud.define("cities", function(request, response) {
var query = new Parse.Query("user_picture");
query.find({
success: function(results) {
    var cities = new Array();

    for (var object in results){
        var tempArray = [object.get("city")];

        if (cities.length > 0){
            for (var i = 0; i < cities.length; i++){
                if (cities[i].get("city") == object.get("city")) {
                    break;
                } else if (i == cities.length-1) {
                    cities = cities.concat(tempArray);
                }
            }
        }
    }   
    response.success (cities);
}, error: function() {
    response.error("Error");
}
});});

However, when i run this function i receive the following error:

Error: TypeError: Object 0 has no method 'get'
at query.find.success (main.js:15:30)
at Parse.js:2:5786
at r (Parse.js:2:4981)
at Parse.js:2:4531
at Array.forEach (native)
at Object.E.each.E.forEach [as _arrayEach] (Parse.js:1:666)
at n.extend.resolve (Parse.js:2:4482)
at r (Parse.js:2:5117)
at Parse.js:2:4531
at Array.forEach (native) (Code: 141, Version: 1.2.18)

And the response returns null. I tried printing one object from the results array in order to make sure i'm receiving the right query, and it's printing fine the city. What could be the problem?

4
  • 1
    What kind of object is results? It's telling you it has no method get. Maybe you mean object.city? Commented Feb 14, 2014 at 15:51
  • Which line causes the error? Commented Feb 14, 2014 at 15:54
  • @ppoliani i couldn't identify the error as the code is running on Parse.com servers. Commented Feb 14, 2014 at 15:57
  • @Mathletics results is of type Array. Each object in it is of type dictionary (iOS speaking). i tried to return results[0].get("city") it returns fine the city for the specified object Commented Feb 14, 2014 at 15:57

2 Answers 2

1

The for in loop iterates through all the keys of an object literal. Since results is an Array it will iterate through the keys of the Array, which are '0', '1' etc.

This means that the object variable will hold those key vales. And since they are not objects they don't have a method called get.

You need a forEach loop instead.

results.forEach(function(object){
    var tempArray = [object.get("city")];

    if (cities.length > 0){
        for (var i = 0; i < cities.length; i++){
            if (cities[i].get("city") == object.get("city")) {
                break;
            } else if (i == cities.length-1) {
                cities = cities.concat(tempArray);
            }
        }
    }
}  

});

Or if you're targeting ES3 then you should use a for loop

for(var i = 0, length = results.length; i< length; i++){
    var object = results[i];

    var tempArray = [object.get("city")];

    if (cities.length > 0){
        for (var i = 0; i < cities.length; i++){
            if (cities[i].get("city") == object.get("city")) {
                break;
            } else if (i == cities.length-1) {
                cities = cities.concat(tempArray);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I recall working with Parse objects a bit and there seemed to be times to access them as an object (by direct parameter access) and sometimes by using the get method and it looks like you're mixing up the array access and object (from Parse) access methods.

Also, your list generator doesn't seem like it's really building a unique list. You only check to see if the current city is the same as the city you're going to add.

I might do something more like this (for the success method):

function(parseResults) {
  var cities = {};
  var ii=0;
  var nResults = parseResults.length
  for(;ii<nResults;++ii) {
    cities[result.get('city')] = 1
  }
  var citiesArray = cities.keys();
  response.success(citiesArray);
}

What we do here is build up an object whose keys are city names. Then return the keys as an array. What this does for us is automatically build a unique list because object keys should be unique.

If the result.get gives you problems, try replacing it with result.city. But i suspect the error you were seeing with your first example was trying to call get on the Array element.

2 Comments

Why do you provide an example that uses a specific library? There is not need to include underscore.js in your project just to iterate through an array.
Point taken. I've updated the code snippet to use vanilla JS. This code only assumes a modern javascript (ECMAScript 5) which includes the keys() method.

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.