如何返回正在返回的对象实例的属性?

时间:2014-05-22 16:30:49

标签: javascript

My Parse云代码有一个查询,以对象实例的形式返回结果。例如,如果查询找到一个匹配项,则返回userCategory的实例及其所有属性。

我想要做的是引用返回的"User categories that match search"实例的某些属性,并返回这些属性,例如minPricemaxPriceitemLocation和{ {1}}。这样,我可以在接收此数据的iOS应用中使用它们。让我知道我是否可以更清楚地了解任何事情。

编辑:我尝试过这样的for循环,它返回“undefined”:

itemCondition

编辑2 :这是在Parse日志中打印的内容:

for (var i = 0; i < userCategoriesMatchingTop2.length; i++)
              {
                console.log('hello' + userCategoriesMatchingTop2[i].itemCondition);
              }

这是功能:

I2014-05-22T18:59:35.059Z] v224: Ran cloud function eBayCategorySearch for user jKs5QpXjpd with:
  Input: {"item":"iphone 5 16gb"}
  Result: {"results":[{"Number of top categories":1},{"Top category Ids":["9355"]},{"Top category names":["Cell Phones & Smartphones"]},{"Number of matches":1},{"User categories that match search":[{"parent":{"__type":"Pointer","className":"_User","objectId":"jKs5QpXjpd"},"categoryId":"9355","itemCondition":"new","itemLocation":"US","maxPrice":"400","minPrice":"250","objectId":"AgsUJ2PjTA","createdAt":"2014-05-15T20:45:32.811Z","updatedAt":"2014-05-15T20:46:37.466Z","__type":"Object","className":"userCategory"}]}]}
I2014-05-22T18:59:36.209Z] Top category Ids: 9355
I2014-05-22T18:59:36.210Z] Top category Names: Cell Phones & Smartphones
I2014-05-22T18:59:36.313Z] helloundefined
I2014-05-22T18:59:36.313Z] userCategory comparison success!
I2014-05-22T18:59:36.314Z] [{"parent":{"__type":"Pointer","className":"_User","objectId":"jKs5QpXjpd"},"categoryId":"9355","itemCondition":"new","itemLocation":"US","maxPrice":"400","minPrice":"250","objectId":"AgsUJ2PjTA","createdAt":"2014-05-15T20:45:32.811Z","updatedAt":"2014-05-15T20:46:37.466Z","__type":"Object","className":"userCategory"}]
I2014-05-22T18:59:36.315Z] User categories that match search: [object Object]

以下是返回时数据的外观:

Parse.Cloud.define("eBayCategorySearch", function(request, response) {
          url = 'http://svcs.ebay.com/services/search/FindingService/v1';

  Parse.Cloud.httpRequest({
      url: url,
      params: {     
       'OPERATION-NAME' : 'findItemsByKeywords', 
       'SERVICE-VERSION' : '1.12.0',
       'SECURITY-APPNAME' : '*App ID goes here*',
       'GLOBAL-ID' : 'EBAY-US',
       'RESPONSE-DATA-FORMAT' : 'JSON',
       'itemFilter(0).name=ListingType' : 'itemFilter(0).value=FixedPrice',
       'keywords' : request.params.item,

     },
      success: function (httpResponse) {


  // parses results

          var httpresponse = JSON.parse(httpResponse.text);
          var items = [];

          httpresponse.findItemsByKeywordsResponse.forEach(function(itemByKeywordsResponse) {
            itemByKeywordsResponse.searchResult.forEach(function(result) {
              result.item.forEach(function(item) {
                items.push(item);
              });
            });
          });


  // count number of times each unique primaryCategory shows up (based on categoryId), returns top two IDs and their respective names


          var categoryIdResults = {};

          // Collect two most frequent categoryIds
          items.forEach(function(item) {
            var id = item.primaryCategory[0].categoryId;
            if (categoryIdResults[id]) categoryIdResults[id]++;
            else categoryIdResults[id] = 1;
          });

          var top2 = Object.keys(categoryIdResults).sort(function(a, b) 
            {return categoryIdResults[b]-categoryIdResults[a]; }).slice(0, 2);
          console.log('Top category Ids: ' + top2.join(', '));


          var categoryNameResults = {};

          // Collect two most frequent categoryNames  
          items.forEach(function(item) {
            var categoryName = item.primaryCategory[0].categoryName;
            if (categoryNameResults[categoryName]) categoryNameResults[categoryName]++;
            else categoryNameResults[categoryName] = 1;
          });  


          var top2Names = Object.keys(categoryNameResults).sort(function(a, b) 
            {return categoryNameResults[b]-categoryNameResults[a]; }).slice(0, 2);
          console.log('Top category Names: ' + top2Names.join(', '));



  // compare categoryIdResults to userCategory object

          //Extend the Parse.Object class to make the userCategory class
          var userCategory = Parse.Object.extend("userCategory");

          //Use Parse.Query to generate a new query, specifically querying the userCategory object.
          query = new Parse.Query(userCategory);

          //Set constraints on the query.
          query.containedIn('categoryId', top2);
          query.equalTo('parent', Parse.User.current())

          //Submit the query and pass in callback functions.
          var isMatching = false;
          query.find({
            success: function(results) {
              var userCategoriesMatchingTop2 = results;
              console.log("userCategory comparison success!");
              console.log(results);
              if (userCategoriesMatchingTop2 && userCategoriesMatchingTop2.length > 0) {
                isMatching = true;
              }

              response.success({
                "results": [
                  { "Number of top categories": top2.length },
                          { "Top category Ids": top2 },
                        { "Top category names": top2Names },   
                         { "Number of matches": userCategoriesMatchingTop2.length }, 
         { "User categories that match search": userCategoriesMatchingTop2 },
                ]
              });

              console.log('User categories that match search: ' + results)
            },
            error: function(error) {
              //Error Callback
              console.log("An error has occurred");
              console.log(error);
            }
          });
  },
          error: function (httpResponse) {
              console.log('error!!!');
              response.error('Request failed with response code ' + httpResponse.status);
          }
     });
});

0 个答案:

没有答案