AngularJS + Parse API调用服务不受userId的约束

时间:2014-12-02 20:41:28

标签: javascript angularjs rest parse-platform

对于我的应用,我已经为地址创建了一项服务,它允许我为任何给定用户操作地址对象。除了我的标准CRUD函数,我需要有一个函数来列出指定Parse.User的任何地址。

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

在我的Angular模板中,视图确实返回Address对象。但它返回 all 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对象?

回答我不是在寻找:

  

返回所有Address对象,只保存与Parse.User.current()。id匹配的对象到$ scope。

1 个答案:

答案 0 :(得分:1)

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

如果userId的数据类型为Pointer,您应该写如下:

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