2

I have a class Store that has a relation itemsInStore that contains multiple Item objects on Parse. I'm currently trying to Parse Cloud Code to retrieve all Items in a Store, but don't know what a common practice for this is.

In particular, I couldn't find articles that really answer my question here, e.g. how the query should be.

Parse.Cloud.define("retrieveNearbyLocationsAndItemsWithCurrentLocation", function(request, response) {
    var Store = Parse.Object.extend("Store");
    var store = new Store();
    var query = new Parse.Query(Store);
    var userGeoPoint = new Parse.GeoPoint(request.params.lat, request.params.long);

    query.near("geopoint", userGeoPoint);
    query.limit(1);

    query.find({
        success: function(httpResponse) {
            response.success(httpResponse[0].get("itemsInStore"));
            console.log(httpResponse[0].get("itemsInStore"));

        },
        error: function(httpResponse) {
            response.error("Error: retrieveNearbyLocationsAndItemsWithCurrentLocation");
        }


    });


});

Console log would return {"__type":"Relation","className":"Item"}

In order to dig into this Relation and retrieve all Item objects in it, what should be done next?

udjat
  • 479
  • 2
  • 10
  • 1
    `var query = aStore.get("itemsInStore").query();` – danh Apr 02 '15 at 05:23
  • @danh thanks for the hint! However I kept getting errors like `Error: Can't serialize an unsaved Parse.Object`, not sure what to do here. Thanks :) – udjat Apr 02 '15 at 06:42
  • 1
    It was worthwhile to post the code --- I added an answer explicit about how to use the relation column. The error message could be the result of trying to return the relation, rather than the results of it's query. (Never tried to return just a PFRelation... Poor error message either way, since there's no attempt to save anything). – danh Apr 02 '15 at 14:06

1 Answers1

1

The relation answers a query. Run that query to get the related elements.

Parse.Cloud.define("retrieveNearbyLocationsAndItemsWithCurrentLocation", function(request, response) {
    var Store = Parse.Object.extend("Store");
    var store = new Store();
    var query = new Parse.Query(Store);
    var userGeoPoint = new Parse.GeoPoint(request.params.lat, request.params.long);
    query.near("geopoint", userGeoPoint);
    query.limit(1);

    query.find().then(function(stores) {
        if (stores.length && stores[0].relation("itemsInStore")) {
            var itemQuery = stores[0].relation("itemsInStore").query();
            return itemQuery.find();
        } else {
            return [];
        }
    }).then(function(items) {
        response.success(items);
    }, function(error) {
        response.error(error);
    });
});
udjat
  • 479
  • 2
  • 10
danh
  • 62,181
  • 10
  • 95
  • 136
  • thanks for the awesome answer! I would suggest changing `if (stores.length && stores[0].get("itemsInStore"))` to `if (stores.length && stores[0].relation("itemsInStore"))` and the same for the next line since I would get TypeError. – udjat Apr 02 '15 at 14:46