2

I'm new to Parse, and from WP background obviously.

I have 2 classes on Parse: [prop] & [propmeta]
The relation of these classes were like post & post_meta

prop
- addr
- desc

propmeta
- prop(pointer to prop)
- metakey
- metaval

Below is existing query of [prop] returned

{  
   "result":[  
      {  
         "__type":"Object",
         "addr":"taman klang",
         "className":"prop",
         "createdAt":"2015-07-08T07:18:31.101Z",
         "desc":"no desc",
         "objectId":"eZmV76Ie6t",
         "updatedAt":"2015-07-08T08:27:14.924Z"
      },
      {  
         "__type":"Object",
         "addr":"taman cheras",
         "className":"prop",
         "createdAt":"2015-07-08T07:19:17.005Z",
         "desc":"no desc",
         "objectId":"QIEy9AyqtH",
         "updatedAt":"2015-07-08T08:26:56.007Z"
      }
   ]
}

My Cloud Code query

Parse.Cloud.define("prop_get", function(request, response) {
  var limit   = request.params.limit;
  var skip    = request.params.skip;
  var orderby = request.params.orderby;
  var order   = request.params.order;

  var prop        = Parse.Object.extend("prop");
  var query       = new Parse.Query(prop);
  query.find({
    success: function(results) {
      response.success(results);
    },
    error: function() {
      console.log("error: " + error);
    }
  });

});

What I want to achieve is query [prop] with [propmeta] child nodes appended and return as 1 json result. (Since Parse charges by API call, this becomes a requirement immediatly)

Kindly advise the amend on cloud code (preferable) or change to proper schema or any solution which would achieve the desired result.

Thank you.

kRiZ
  • 2,320
  • 4
  • 28
  • 39
Ed On
  • 36
  • 4

1 Answers1

0

Why don't have the propmeta pointer in the propclass instead of the other way around like you have at the moment. You would then be able to query like below. Just use the include():

var prop        = Parse.Object.extend("prop");
var query       = new Parse.Query(prop);
query.include("prop_meta_column_name");
query.find({.....});

But if you don't want to change the Parse class structure, you'll have to query for your propmeta class instead, while using include for your prop pointer column:

var propmeta        = Parse.Object.extend("propmeta");
var query       = new Parse.Query(propmeta);
query.include("prop_column_name");
query.find({.....});
kRiZ
  • 2,320
  • 4
  • 28
  • 39
  • appreciate your answer, – Ed On Jul 10 '15 at 07:15
  • I tried reverse the schema as your advise, added relation(propmeta) to [prop] and include query.include("propmeta"); but query.include not working for relation column as error return: {"code":141,"error":"field propmeta cannot be included because it is not a pointer to another object"} *Since [prop] -> [propmeta] = 1 -> many, add a pointer(propmeta) to [prop] doesn't fit the relationship. – Ed On Jul 10 '15 at 07:27
  • Okay... Didn't know about the 1 -> many detail. So one `prop` object can have many `propmeta` objects. The current setup is fine then. The second query should work fine. – kRiZ Jul 10 '15 at 07:33
  • 2nd query working fine definitely with return of [propmeta] as parent, [prop] as child node, but the tricky(for me) part is want to query return [prop] as parent, [propmeta] as child node. Any idea? or this is infeasible on Parse.com? – Ed On Jul 10 '15 at 07:40
  • So are you trying to get the `propmeta` for one particular `prop` in the query? – kRiZ Jul 10 '15 at 07:42
  • So you want to get all the props with their related propmeta data. The thing with the one-to-many relationship is that you'll have to base the query on the many side, since that is the side which knows about the relating field. – kRiZ Jul 10 '15 at 08:23
  • Ya, exactly. how to achieve this on cloud code? btw, really appreciate your patient kRiZ. – Ed On Jul 10 '15 at 12:14
  • If you use pointers for one-to-many relationship, like you are at the moment, you'll have to use the second query above. I you want to query on the `prop` table instead, you should use `PFRelation` or array column instead of pointer. Using `PFRelation will be tricky since you'll have to do a new query for every object you retrieve. But if there aren't any specific requirements to have the meta a child of prop, then stick with the current query. – kRiZ Jul 10 '15 at 12:21
  • Greatly appreciate yr reply. tried query relation by referring google/stackoverflow last night, but no luck producing the desired result. Do you have working piece of cloud code which query relation which produce the desired result? willing to purchase if working. :P – Ed On Jul 11 '15 at 04:15