2014-12-02 61 views
1

对于我的应用程序,我创建了一个服务地址,它允许我操作任何给定用户的地址对象。除了我的标准CRUD函数外,我还需要一个函数来列出指定Parse.User的任何地址。AngularJS +解析API调用服务不受userId约束

services.js

.factory('Address',['$http', 'PARSE_CREDENTIALS', function ($http,PARSE_CREDENTIALS) { 
    return { 
     // constrain to User ID 
     getAll: function(userId) { 
      return $http.get('https://api.parse.com/1/classes/Address', { 
       headers: { 
        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID, 
        'X-Parse-REST-API-Key': PARSE_CREDENTIALS.REST_API_KEY, 
        'Content-Type' : 'application/json' 
       }, 
       params: { "userId": userId } 
      }); 
     }, 
     // ...get(), edit(), add(), delete() 

controllers.js

.controller('AddrCtrl', ['$scope', '$state', '$stateParams', '$rootScope', 'Address', 
function($scope, $state, $stateParams, $rootScope, Address) { 
     Address.getAll($rootScope.user.id) 
     .success(function(data) { 
      console.log(data); 
      $scope.addresses = data.results; 
     }) 
}]); 

在我的角模板,这个视图返回地址的对象。但它返回全部 Address对象,它应该只返回具有相应userId的Address对象。为了说明,Address类有一个userId指针列。每个地址只有一个用户。

这里是日志消息AddrCtrl返回控制台:

Object {results: Array[2]} 
    results: Array[2] 
     0: Object 
      firstName: "(test)" 
      lastName: "test" 
      // more unrelated properties 
      objectId: "yUEuFjLlzs" 
      updatedAt: "2014-12-02T20:17:55.608Z" 
      userId: Object 
       __type: "Pointer" 
       className: "_User" 
       objectId: "q1KADkp4i1" 

我假设的问题在于我的$ http.get()函数的地方。最终,我的问题是这样的:为什么我的params选项不会将我的data.results限制为仅与一个Parse.User关联的Address对象?

答案我不是在寻找:

返回所有地址对象,并只保存那些匹配Parse.User.current()ID为$范围。

回答

1

您需要使用where子句来执行查询。

如果userId数据类型是Pointer,你应该写如下:

{"where": JSON.stringify({ 
    "userId": {"__type":"Pointer","className":"_User","objectId":"userId"}} 
)}