简体   繁体   中英

Parse Cloud Code Function Response Success is not a function

I'm trying to call a Parse Cloud Function from an iOS client but the response.success() function seems to be null. I am getting an error saying that response.success is not a function on the server.

This is my Parse Cloud Function:

Parse.Cloud.define("pruebaQuery", function(request, response) {
  const query = new Parse.Query("grupo");
  query.equalTo("name", request.params.grupoName);
  query.find()
    .then((results) => {
      for (let i = 0; i < results.length; ++i) {
        var grupoId = results[i].get("grupoId");
        console.log("GrupoId: " + grupoId);
      }
      response.success("Success pruebaQuery");
    })
    .catch(() =>  {
      response.error("grupo lookup failed");
    });
});

This is how I call it from the iOS client:

[PFCloud callFunctionInBackground:@"pruebaQuery" withParameters:@{@"grupoName": @"Kinder 3"} 
block:^(NSString *object, NSError *error) {
        if (!error) {
            NSLog(@"CLOUDCode/SUCCESS: %@", object);
        }
        else {
            NSLog(@"CLOUDCode/ERROR %@  code: %ld", error, (long)[error code]);
        }
    }];

Any clues why the response.success() function is not working?

Since parse-server version ^3.0.0, cloud codes no longer use callbacks. You can use promise or async functions. Here is how you should change your cloud function.

Parse.Cloud.define("pruebaQuery", async request=> {
    const query = new Parse.Query("grupo");
    query.equalTo("name", request.params.grupoName);

    let results;
    try{
        results = await query.find();

        for (let i = 0; i < results.length; ++i) {
        let grupoId = results[i].get("grupoId");
        console.log("GrupoId: " + grupoId);
       }

    } catch(error){
        throw error.message;
    }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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