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

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

问题描述

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

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数据浏览器中,播放器"对象有一列标记为:

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