parse-community / parse-php-sdk

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

Warning: Invalid argument supplied for foreach() in src/Parse/ParseObject.php #375

Closed cubycode closed 6 years ago

cubycode commented 6 years ago

Issue Description

Hi there, I'm getting this issue while loading my websites with Parse PHP SDK today, it's been an hour so far. I also get this error about ParseQuery.php: Warning: Invalid argument supplied for foreach() in src/Parse/ParseQuery.php

Steps to reproduce

Just try this website: http://fvimagination.com/events/index.php, it seems there's an issue on ParseObject.php

Environment Details

ghost commented 6 years ago

Try printing your error, it will give you exact line number in which foreach is throwing error. And then you can find out what argument you are passing which is invalid, several reasons can be associated with invalid argument, eg. not an array, empty, null etc.

cubycode commented 6 years ago
  1. I don't know how to print an error of the Parse SDK
  2. Sometimes it goes smoothly and runs the page, other times it throws that Warning, so I guess there's something wrong in the SDK, not in my code because it was working perfectly until 2 hours ago...
cubycode commented 6 years ago

Btw, here are the lines numbers:

Warning: Invalid argument supplied for foreach() in src/Parse/ParseObject.php on line 634

Warning: Invalid argument supplied for foreach() in src/Parse/ParseObject.php on line 670

Warning: Invalid argument supplied for foreach() in src/Parse/ParseObject.php on line 634

Warning: Invalid argument supplied for foreach() in src/Parse/ParseObject.php on line 670
ghost commented 6 years ago

So are u calling "_mergeAfterFetch" of parse SDK, from ur php file?

ghost commented 6 years ago

Try putting try catch on the function .. in ur php file ( and not SDK file ) and in catch u can print_r error

try
    {
     ur code
    }
    catch (Exception $e)
    {
print_r($e);
}
cubycode commented 6 years ago

I already did that for my query in my index.php file, but it doesn't print any error, just those warnings relative to ParseObject, so not my code but the Parse PHP SDK code...

cubycode commented 6 years ago

So are u calling "_mergeAfterFetch" of parse SDK, from ur php file?

No, I never used that command in my code.

ghost commented 6 years ago

You will have to find out which method you are calling, which is giving u error!

cubycode commented 6 years ago

This is what I call in the first block, as soon as the page gets loaded:

   // Get parse Object by its objectID
    $eObjID = $_GET['eObjID'];

     try {
      $eObj = new ParseObject("Events", $eObjID);
      $eObj->fetch();

      // Get title
      $title = $eObj->get('title');
      echo $title;

    // error 
    } catch (ParseException $e){ echo $e->getMessage(); }

This code was working fine until 2 hours ago, it's all I know, because it doesn't print any error in the catch function.

ghost commented 6 years ago
// Get parse Object by its objectID
    $eObjID = $_GET['eObjID'];

     try {
      $query = new ParseQuery("Events");
      $query->equalTo("objectId",$eObjID);
      $eObj = $query->first();

      // Get title
      $title = $eObj->get('title');
      echo $title;

    // error 
    } catch (ParseException $e){ echo $e->getMessage(); }

Can u try using this? Make sure $_GET['eObjID'] has an objectId, if this works for u, I will tell u the bugs.

cubycode commented 6 years ago

I did, and it actually works, so do you mean that the fetch() function doesn't work anymore on Parse PHP SDK?

 $eObj = new ParseObject("Events", $eObjID);
 $eObj->fetch();
cubycode commented 6 years ago

UPDATE: It stopped working again:

Warning: Invalid argument supplied for foreach() in /home/fvima186/public_html/events/src/Parse/ParseQuery.php on line 603

Fatal error: Call to a member function get() on array in /home/fvima186/public_html/events/event-details.php on line 46

Here's my code:

// Get parse Object by its objectID
    $eObjID = $_GET['eObjID'];

     try {
      $query = new ParseQuery("Events");
      $query->equalTo("objectId", $eObjID);
      $eObj = $query->first();

      // Get title
      $title = $eObj->get('title');

      // Get description
      $description = $eObj->get('description');

      echo '
        <title>' .$title. '</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="' .$description. '">
    ';

    // error 
    } catch (ParseException $e){ echo $e->getMessage(); }

line 46 is this one: $title = $eObj->get('title');

ghost commented 6 years ago

Looks like ur $eObj is empty, try printing it using print_r($eObj), if empty it will print empty array[], that means either u r providing wrong objectId or name of model 'Events' is wrong!

cubycode commented 6 years ago

I could successfully use fetch(); since 2 hours ago, it's been 2 months I'm using it :) Anyway, my site stopped working even with the ParseQuery you posted above, I'm also launching a query in index.php, and it throws the same warning, so now even the ParseQuery is not working fine...

cubycode commented 6 years ago

My $eObj is not empty, it's a $_GET variable:

$eObjID = $_GET['eObjID'];
print_r($eObjID);
// prints: iUqVPNtvoo
ghost commented 6 years ago

Talking about $eObj and not $eObjID.. :)

cubycode commented 6 years ago

yep, it successfully gets printed out (the $eObj pare object) :)

Parse\ParseObject Object ( [serverData:protected] => Array ( [title] => LAST: Wedding Event in NYC [description] => lorem ipse dolor quantum lorem ipse dolor quantum lorem ipse dolor quantum lorem ipse dolor quan etc...

cubycode commented 6 years ago

so, it seems the issue comes out when i add this line:

$title = $eObj->get('title');

if i comment it out, and i print $eObj, it works smoothly even with the fetch() function...

cubycode commented 6 years ago

I confirm this is an issue with the 1.3.0 version of this SDK because I've just uploaded an older version of the src/Parse folder on my server and my website is working perfectly.

ghost commented 6 years ago

I see, I m still using, 1.2.0

cubycode commented 6 years ago

actually, i’ve discovered that it may be a back4app server issue, because i’ve hosted Parse Server on Heroku and it works smoothly, so i’m gonna close this issue since it seems that Parse php sdk is fine. thanks for your help anyway!

montymxb commented 6 years ago

Glad to see you've managed to resolve it @fvimagination . For future reference first will return an empty array if no object is found, rather than the expected object (or null). Be sure to sanity check what you're getting back from a query before you assume it's a valid object, especially from first. It's likely that the eObjID you were expecting to match was not actually valid in one case or another.

cubycode commented 6 years ago

It simply was a back4app strange server issue, I've contacted those guys and they fixed it :)