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

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

问题描述

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

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

此代码输出例如,对于ID为'xyz'的特定用户,有0张图片。但是,我可以看到用户存储了大量图片。

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天全站免登陆