1

For my app, I've created a service for Address, which allows me to manipulate Address objects for any given user. Aside from my standard CRUD functions, I need to have one function to list any address for a specified 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;
        })
}]);

In my Angular template, the view does return Address objects. But it returns all the Address objects when it should only be returning the Address objects with a corresponding userId. To clarify, the Address class has a userId pointer column. Each address only has one User.

Here is the log message that AddrCtrl returns in the console:

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"

I'm assuming that the issue lies somewhere in my $http.get() function. Ultimately, my questions is this: why does my params option not constrain my data.results to just the Address objects associated with one Parse.User?

Answer I am not looking for:

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

1 Answer 1

1

You need to use where clause to perform the query.

If the data type of userId is Pointer, you should write as following:

{"where": JSON.stringify({
    "userId": {"__type":"Pointer","className":"_User","objectId":"userId"}}
)}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.