javascript - AngularJS + Parse API 调用服务不受 userId 的限制

标签 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 模板中, View 确实返回 Address 对象。但它返回所有 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 对象?

我不是在寻找答案:

Return all Address objects and only save the ones matching Parse.User.current().id into $scope.

最佳答案

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

如果userId的数据类型是Pointer,则应该这样写:

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

关于javascript - AngularJS + Parse API 调用服务不受 userId 的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258536/

相关文章:

javascript - 获取实时 .html 进行比较

javascript - Jest - 理解 describe() 和 it() 的执行顺序

asp.net-mvc - 将 SPA 分割为多个组件并使用 AngularJS

java - 如何通过 Java 服务器端的 main() 方法创建 JSON、RESTful 服务

spring - 如何在 spring boot 中从同一个应用程序调用另一个 api

javascript - Ember 错误 : Adapter operation failed

javascript - ng-show 在 ng-repeat 中使用多个过滤器

javascript - 范围更改不会出现在子范围中

angularjs - Symfony 2 中 AngularJS 模板位置的最佳实践和新的 Angular Router?

java - 在 java REST 中解析 JSON 参数时出错