tutchmedia
tutchmedia

Reputation: 139

Parse Cloud Code - Await and return relation query

so I'm trying to get this cloud query function to run. Basically, I would like to get return the profile by its ID. Then using that result object, run a relation query and return the first result.

I'm not sure if I'm getting mixed up but I'm struggling on awaiting for the full query to finish before returning.

Thanks

Parse.Cloud.define("getLastWeightForAnimal", async (request) => {

try {

    var AnimalProfiles = Parse.Object.extend("animal_profiles");
    var query = new Parse.Query(AnimalProfiles);

    query.get(request.params.id).then((animalProfile) => {

        var AnimalWeights = animalProfile.relation("weights").query();

        AnimalWeights.descending("createdAt");

        let result = await AnimalWeights.first();
        return result;

    }, (error) => {
      // The object was not retrieved successfully.
      // error is a Parse.Error with an error code and message.
        console.log("Uh Oh Inner");
        console.log("Error Inner: "+  error);
    });

} catch (e) {
    console.log("Uh Oh");
    console.log("Error: "+  e);
}


});

Upvotes: 0

Views: 1147

Answers (1)

Cody Geisler
Cody Geisler

Reputation: 8617

If you return inside of a promise .then(function(){return 'a'}) thereturn 'a'does not return theasync (request)` !

If you do

Promise.resolve()
  .then(function(){return 'a'}) // this 'a' does not go to any parent function!
  .then(function(val){console.log(val)}) // it goes here!

you would see 'a' in your log, as a simple illustration.

You can switch it to async/await

Parse.Cloud.define("getLastWeightForAnimal", async (request) => {

  try {

    var AnimalProfiles = Parse.Object.extend("animal_profiles");
    var query = new Parse.Query(AnimalProfiles);

    var animalProfile = await query.get(request.params.id)

    var AnimalWeights = animalProfile.relation("weights").query();

    AnimalWeights.descending("createdAt");

    let result = await AnimalWeights.first();
    return result;

  } catch (e) {
    console.log("Uh Oh");
    console.log("Error: "+  e);
  }


});

OR simply return the promise, which since you're using async will automatically return the value of the promise.

Parse.Cloud.define("getLastWeightForAnimal", async (request) => {

  try {

    var AnimalProfiles = Parse.Object.extend("animal_profiles");
    var query = new Parse.Query(AnimalProfiles);

    // note new return!!
    return query.get(request.params.id).then((animalProfile) => {

        var AnimalWeights = animalProfile.relation("weights").query();

        AnimalWeights.descending("createdAt");

        let result = await AnimalWeights.first();
        return result;

    }, (error) => {
      // The object was not retrieved successfully.
      // error is a Parse.Error with an error code and message.
        console.log("Uh Oh Inner");
        console.log("Error Inner: "+  error);
    });

  } catch (e) {
    console.log("Uh Oh");
    console.log("Error: "+  e);
  }


});

Upvotes: 1

Related Questions