使用 Javascript 在 Parse.com 中获取多个相关对象的简单方法? [英] Simple way to get multiple related objects in Parse.com with Javascript?

查看:14
本文介绍了使用 Javascript 在 Parse.com 中获取多个相关对象的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Player 类.玩家可以拥有 x 个奖杯.我有玩家对象 ID,需要获取他们所有奖杯的列表.

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.

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

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

trophies Relation<Trophy>
(view Relations)

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

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

我在内存中有 ParseObject 'player':

I have the ParseObject 'player' in memory:

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

我也尝试过关系查询:

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.

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

Anyone know how to do this in Javscript CloudCode?

非常感谢!

编辑--

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

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.

推荐答案

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

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

})

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

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.com Javascript SDK 的绊脚石 - 一个没有找到任何东西的查询仍然被认为是成功的.所以每次查询时,都必须检查响应的长度是否大于 0,因为成功的查询可能什么都不返回.

Another stumbling block about 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.

这篇关于使用 Javascript 在 Parse.com 中获取多个相关对象的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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