parse-community / parse-php-sdk

The PHP SDK for Parse Platform
https://parseplatform.org/
Other
812 stars 346 forks source link

Cloud Code Response Error #517

Closed YousefAlsbaihi closed 1 year ago

YousefAlsbaihi commented 1 year ago

New Issue Checklist

Issue Description

I create a cloud function that create users and i called it using PHP, not it's creating users fine and all good in Parse but the response is not there, all i get is error

A PHP Error was encountered
Severity: Warning

Message: Undefined array key "result"

Filename: Parse/ParseCloud.php

Line Number: 40

Steps to reproduce

Create a cloud function using this code

Parse.Cloud.define("createNewUser", async (request) => {
    var User = Parse.Object.extend("User");
    var us = new User();
    us.set("username", request.params.username);
    us.set("name", request.params.name);
    us.set("email", request.params.email);
    us.set("password", request.params.password);
    await us.save()
        .then((user) => {
            // Execute any logic that should take place after the object is saved.
            return ('New object created with objectId: ' + user.id);
        }, (error) => {
            // Execute any logic that should take place if the save fails.
            // error is a Parse.Error with an error code and message.
            return ('Failed to create new object, with error code: ' + error.message);
        });
});

// To Run the code in PHP 

$rating = ParseCloud::run("newUser", $userData);

Actual Outcome

Error as explained before

Expected Outcome

fail or success message

Environment

Parse PHP SDK

Server

Database

Logs

A PHP Error was encountered
Severity: Warning

Message: Undefined array key "result"

Filename: Parse/ParseCloud.php

Line Number: 40

Backtrace:

File: /Applications/MAMP/htdocs/scr/application/third_party/parse/vendor/parse/php-sdk/src/Parse/ParseCloud.php
Line: 40
Function: _error_handler
parse-github-assistant[bot] commented 1 year ago

Thanks for opening this issue!

YousefAlsbaihi commented 1 year ago

It's resolved, it turned out that i have to return the whole save() function, so the cloud code would be:

Parse.Cloud.define("createNewUser", async (request) => {
    var User = Parse.Object.extend("User");
    var us = new User();
    us.set("username", request.params.username);
    us.set("name", request.params.name);
    us.set("email", request.params.email);
    us.set("password", request.params.password);
    return await us.save();
});

i'm not sure if this is secure or not, but managed to get objectId and Error messages if there is any, however i was confused of the example here

Parse.Cloud.define("averageStars", async (request) => {
  const query = new Parse.Query("Review");
  query.equalTo("movie", request.params.movie);
  const results = await query.find();
  let sum = 0;
  for (let i = 0; i < results.length; ++i) {
    sum += results[i].get("stars");
  }
  return sum / results.length;
});

this example return a string, how Parse Cloud Php would get result from that ? i haven't tested that example but just observation.

if there is a different approach, i would appreciate it please let me know.

mtrezza commented 1 year ago

A Cloud Function can return any type, if I'm not mistaken, it doesn't even have to be a JSON. It then depends on your implementation client side to properly handle the response. So the Parse PHP SDK should not expect a certain type for a Cloud Function response.

mtrezza commented 1 year ago

I'm closing this as it does not seem to be a Parse PHP SDK issue.