简体   繁体   English

解析云代码-等待并返回关系查询

[英]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. 基本上,我想通过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);
}


});

If you return inside of a promise .then(function(){return 'a'}) the return 'a' does not return the async (request)` ! 如果您在promise .then(function(){return 'a'}) the返回, .then(function(){return 'a'}) the返回'a' does not return the异步(请求)`!

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. 您将在日志中看到“ a”,这是一个简单的例子。

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. 或简单地返回promise,由于您使用的是async它将自动返回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);
  }


});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM