Parse.com:查找属于具有 objectId 的用户的所有对象 [英] Parse.com: Find all objects belonging to a user with objectId

查看:20
本文介绍了Parse.com:查找属于具有 objectId 的用户的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在解析的类,比如图片.每一个都属于一个用户.对此用户的引用存储在 Pictures 表/类中,作为指向用户的 Pointer.

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.

在我的云代码中,我试图使用主密钥获取属于某个用户的所有图片.以下是我的代码:

In my cloud code I am trying to get all Pictures belonging to a user, using master key. Following is my code:

Parse.Cloud.define("getPictures", function(request, response) {

  Parse.Cloud.useMasterKey();

  var query = new Parse.Query("Pictures");

  query.equalTo("user", request.params.user);

  query.find({
    success: function(results) {

      var status = "Found " + results.length + " pictures for userId " + request.params.user;
      response.success(status);

    },

    error: function() {

      status = "No pictures exist for userId " + request.params.user; 
      response.error(status);
    }
  });
});

此代码输出例如某个用户有 0 张图片,id 为 'xyz'.但是,我可以看到用户存储了很多图片.

This code outputs that there are 0 pictures for a certain user with id 'xyz' for example. However, I can see that the user has a lot of pictures stored.

我还验证了问题不在于使用主密钥,正如我在控制台日志中看到的那样,代码正在作为主密钥执行.而且,如果我通过objectId查询一张图片,它确实会出现在结果中,这意味着ACL不是这里的问题.

I have also verified that the problem is not with using master key, as I see in the console log that the code is being executed as master. Moreover, if I query for a picture by objectId, it does come out in the results, which means ACL is not the problem here.

我想我必须在这里使用关系/加入,但我不知道该怎么做.

I think I have to use relations/joining here, but I am not sure how to do that.

推荐答案

指针作为对象存储在 Parse 数据库中,因此如果您尝试使用 query.equalTo() 将字符串与对象进行比较函数,什么都找不到.这是指针的存储方式:

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

无论如何,似乎如果您有很多与用户相关的图片,您可能正在搜索解析关系而不是指针:https://www.parse.com/docs/relations_guide

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

这篇关于Parse.com:查找属于具有 objectId 的用户的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆