Parse.com: Find all objects belonging to a user with objectId

后端 未结 3 1355
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 03:58

I have a Class in parse, say Pictures. Each of these belongs to a user. Reference to this user is stored in the Pictures table/class as a Pointer to the user.

相关标签:
3条回答
  • 2020-12-14 04:08

    There may be a problem with your params. If "user" is a pointer, then 'request.params.user' is incorrect, because PFObjects may not be sent as params. If "user" is a pointer, use 'request.user'. If request.params.user is a string of the userId, you could use the Id to reconstruct a PFObject shell before the query as was suggested by Murilo already, but deleting the "user" param and using request.user would shorten your code and not duplicate any values. Murilo's solution is also beneficial because you could pass a userId other than the current user's Id.

    0 讨论(0)
  • 2020-12-14 04:09

    Pointers are stored as objects in Parse database, so if you try to compare a string to an object with query.equalTo() function, nothing will be found. This is how pointers are stored:

    {
        __type: 'Pointer',
        className: '_User',
        objectId: user-object-id
    }
    

    If you are querying a class with pointers and want your result comes with the whole object nested, you should set this in your query:

    var query = new Parse.Query('Pictures');
    query.include('user');
    

    In my queries when I want to search by a pointer column, I compare my user object with the nested user object.

    var user = new Parse.User();
    
    // Set your id to desired user object id
    user.id = your-user-id;
    
    var query = new Parse.Query('Pictures');
    
    // This include will make your query resut comes with the full object
    // instead of just a pointer
    query.include('user');
    
    // Now you'll compare your local object to database objects
    query.equalTo('user', user);
    query.find({
        success: function(userPicture) {
            response.success(userPicture);
        }
    });
    

    Anyway, seems that if you have many pictures related to an user, you probably are searching for parse relations instead of pointers: https://www.parse.com/docs/relations_guide

    0 讨论(0)
  • 2020-12-14 04:16

    If you write a query to retrieve a parent object and a child object to which you have pointer, but no read access as per ACL, then the query may return only parent object and child will be null because the ACL wont let you read it.

    0 讨论(0)
提交回复
热议问题