简体   繁体   English

JS代码返回错误

[英]JS code returning error

I'm new to JS and i had to use it for Cloud Code Parse feature. 我是JS的新手,必须将其用于Cloud Code Parse功能。 I have a class called "user_picture", through the code i query all the objects and go through it's "City" attribute. 我有一个名为“ user_picture”的类,通过代码查询所有对象并通过其“ City”属性。 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. 并且响应返回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? 可能是什么问题呢?

The for in loop iterates through all the keys of an object literal. for in循环遍历对象文字的所有键。 Since results is an Array it will iterate through the keys of the Array, which are '0', '1' etc. 由于results是一个数组,它将迭代数组的键,即“ 0”,“ 1”等。

This means that the object variable will hold those key vales. 这意味着object变量将保存这些关键值。 And since they are not objects they don't have a method called get. 而且由于它们不是对象,所以没有称为get的方法。

You need a forEach loop instead. 您需要一个forEach循环。

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 或者,如果您以ES3为目标,则应使用for循环

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);
            }
        }
    }
}

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. 我回想起使用Parse对象的情况,似乎有时需要将它们作为对象进行访问(通过直接参数访问),有时通过使用get方法来进行访问,看起来您正在混合使用数组访问和对象(来自Parse) )访问方法。

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 . 如果result.get给您带来问题,请尝试将其替换为result.city But i suspect the error you were seeing with your first example was trying to call get on the Array element. 但是我怀疑您在第一个示例中看到的错误是试图调用Array元素上的get

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM