最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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.

In the Parse 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.

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.

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 pletely mon task, but I can't figure out the proper way to do this.

Anyone know how to do this in 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.

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.

In the Parse. 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.

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.

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 pletely mon task, but I can't figure out the proper way to do this.

Anyone know how to do this in 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.

Share Improve this question edited Jul 17, 2013 at 20:08 Hairgami_Master asked Jul 17, 2013 at 20:00 Hairgami_MasterHairgami_Master 5,53911 gold badges46 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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 inplete Parse.Object, and is missing some of the methods required to do this.

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

This is what worked for me:

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

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

And here is another function ing from the other angle:

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

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

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

  • https://parse./questions/how-to-retrieve-parent-objects-with-their-children-in-one-call-javascript-api
  • http://blog.parse./2011/12/06/queries-for-relational-data/
发布评论

评论列表(0)

  1. 暂无评论