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

后端 未结 3 1363
隐瞒了意图╮
隐瞒了意图╮ 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: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

提交回复
热议问题