2014-04-25 76 views
1

我目前有我的雲代碼設置來解析eBay的數據庫時返回前兩個類別ID。如何解析對象的實例並與數組的內容進行比較?

我還在Parse的後端安裝了用戶系統,以便每個User都有與其帳戶關聯的userCategory對象的多個實例。每個userCategory實例都具有唯一的categoryId屬性。

如何,我可以有我的eBayCategorySearch功能迭代通過所有用戶的userCategory情況,並查看是否有有被eBay返回的top2匹配的categoryId財產?我試圖在下面這樣做,但據我所知,這隻會在userCategory是一個數組而不是一個對象的多個實例時才起作用。

函數初始化userCategory對象當用戶註冊:

Parse.Cloud.define("userCategoryCreate", function(request, response) { 
    var userCategory = Parse.Object.extend("userCategory"); 
    var newUserCategory = new userCategory(); 
    newUserCategory.set("categoryId", "9355"); 
    newUserCategory.set("minPrice"); 
    newUserCategory.set("maxPrice"); 
    newUserCategory.set("itemCondition"); 
    newUserCategory.set("itemLocation"); 
    newUserCategory.set("parent", Parse.User.current()); 
    newUserCategory.save({ 

     success: function(){ 
     console.log ('userCategory succesfully created!'); 
     response.success('Request successful'); 
     }, 

     error: function(){ 
     console.log('error!!!'); 
     response.error('Request failed'); 
     } 

    }); 
}); 

eBayCategorySearch功能:

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), return top two 


      var categoryResults = {}; 

      items.forEach(function(item) { 
      var id = item.primaryCategory[0].categoryId; 
      if (categoryResults[id]) categoryResults[id]++; 
      else categoryResults[id] = 1; 
      }); 

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



    // compare categoryResults to userCategory object 

      var userCategory = Parse.User.userCategory; 

      var AnyItemsOfCategoryResultsInUserCategory = Object.keys(categoryResults).some(function(item) { 
      return userCategory.indexOf(item) > -1; 
      }); 
      console.log('Matches found: ' + AnyItemsOfCategoryResultsInUserCategory); 

      var ItemsOfCategoryResultsInUserCategory = Object.keys(categoryResults).filter(function(item) { 
      return userCategory.indexOf(item) > -1; 
      }); 
      console.log('User categories that match search: ' + ItemsOfCategoryResultsInUserCategory) 


      response.success(AnyItemsOfCategoryResultsInUserCategory); 

    }, 
      error: function (httpResponse) { 
       console.log('error!!!'); 
       response.error('Request failed with response code ' + httpResponse.status); 
      } 
    }); 
}); 

回答

0

像這樣的事情?

// Ebay ids (Hopefully you can get to this point. If it's a collection, _.map() etc.) 
var ebayIDs = ['x1','x2']; 

// UserCategoryCollection.models (assuming you're working with a collection .models is where the array of Parse Objects are... here I'm just simply declaring the example) 
var userCategoryCollection.models = [{id:'u1',attributes:{categoryId:'a1'}},{id:'u2',attributes:{categoryId:'x1'}},{id:'u3',attributes:{categoryId:'x2'}}] 

// How to figure out if there is a match... use Underscore 
// Use the find function to iterate through the models, each model being the arg passed 
var found = _.find(userCategoryCollection.models, function(userCategory){ 
    // to the evaluation which will return true if the Parse object prop === a value in the ebayIDs array 
    return _.contains(ebayIDs, userCategory.get('categoryId')); 
}); 

// Evaluate 
if (found) { 
    console.log('hurrah!'); 
} else { 
    console.log('nope'); 
} 

這一點從你的問題,但如果我正確地理解你的問題的概念/方法應該適用抽象。

相關問題