使用 Javascript 在 Parse.com 中获取多个相关对象的简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17709131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:17:14  来源:igfitidea点击:

Simple way to get multiple related objects in Parse.com with Javascript?

javascriptparse-platform

提问by Hairgami_Master

I have a Player class. Players can have x number of Trophies. I have the Player objectId and need to get a list of all of their Trophies.

我有一个播放器类。玩家可以拥有 x 个奖杯。我有 Player objectId 并且需要获取他们所有奖杯的列表。

In the Parse.com data browser, the Player object has a column labeled:

在 Parse.com 数据浏览器中,Player 对象有一列标记为:

trophies Relation<Trophy>
(view Relations)

This seems like it should be so simple but I'm having issues with it.

这看起来应该很简单,但我遇到了问题。

I have the ParseObject 'player' in memory:

我在内存中有 ParseObject 'player':

var query = new Parse.Query("Trophy");
query.equalTo("trophies", player);
query.find({
  /throws an error- find field has invalid type array.

I've also tried relational Queries:

我也试过关系查询:

var relation = new Parse.Relation(player, "trophies");
relation.query().find({
  //also throws an error- something about a Substring being required.

This has to be a completely common task, but I can't figure out the proper way to do this.

这必须是一项完全常见的任务,但我想不出正确的方法来做到这一点。

Anyone know how to do this in Javscript CloudCode?

有人知道如何在 Javscript CloudCode 中做到这一点吗?

Many thanks!

非常感谢!

EDIT--

编辑 -

I can do relational queries on the user fine:

我可以很好地对用户进行关系查询:

var user = Parse.User.current();
var relation = user.relation("trophies");
relation.query().find({

I don't understand why this very same bit of code breaks if I'm using a non-user object.

如果我使用的是非用户对象,我不明白为什么同样的代码会中断。

采纳答案by Hairgami_Master

I finally sorted this out, though there is a caveat that makes this work differently than the documentation would indicate.

我终于解决了这个问题,尽管有一个警告使这项工作与文档所表明的不同。

//Assuming we have 'player', an object of Class 'Player'.

var r = player.relation("trophies");
r.query().find({
  success: function(trophies){
    response.success(trophies); //list of trophies pointed to by that player's "trophies" column.
  },
  error: function(error){
    response.error(error);
  }

})

The caveat:You must have a 'full' player object in memory for this to work. You can't save a player object, grab the object from the success callback and have this work. Some reason, the object that is returned in the success handler is appears to be an incomplete Parse.Object, and is missing some of the methods required to do this.

警告:你必须在内存中有一个“完整”的玩家对象才能工作。您无法保存玩家对象,从成功回调中获取对象并进行这项工作。出于某种原因,成功处理程序中返回的对象似乎是不完整的 Parse.Object,并且缺少执行此操作所需的一些方法。

Another stumbling blockabout the Parse.com Javascript SDK- A query that finds nothing is still considered successful. So every time you query for something, you must check the length of the response for greater than 0, because the successful query could have returned nothing.

Parse.com Javascript SDK 的另一个绊脚石 -没有找到任何内容的查询仍然被认为是成功的。因此,每次查询某些内容时,都必须检查响应的长度是否大于 0,因为成功的查询可能不会返回任何内容。

回答by Ryan

This is what worked for me:

这对我有用:

        var Comment = Parse.Object.extend("Comment");
        var commentsQuery = new Parse.Query(Comment);
        commentsQuery.equalTo("parent", video);//for the given 'video' instance, find its children (comments)
        commentsQuery.descending("createdAt");
        return commentsQuery.find();

Note: of course, videois an instance of the Video class. And returning find()means I'll have to handle the 'promise' in whatever function calls this one.

注意:当然video是Video类的一个实例。返回find()意味着我必须在调用这个函数的任何函数中处理“承诺”。

And here is another function coming from the other angle:

这是另一个角度的另一个函数:

    getRecentCommentsOfAllVideos: function(limit) {
        var Comment = Parse.Object.extend("Comment");
        var commentsQuery = new Parse.Query(Comment);
        commentsQuery.descending("createdAt");
        commentsQuery.limit(limit);
        commentsQuery.include("parent");//this enables the details of the comment's associated video to be available in the result
        return commentsQuery.find();
    }

(Be sure to read https://parse.com/docs/js_guideand search for the line that says "Include the post data with each comment".)

(请务必阅读https://parse.com/docs/js_guide并搜索“在每条评论中包含帖子数据”的行。)

I also looked at these materials; maybe they'll help you (though they weren't much help for me):

我也看了这些材料;也许他们会帮助你(虽然他们对我帮助不大):