0

I am trying to access the value in twig from:

image of data dump

But keep getting an error:

Impossible to access an attribute ("startDate") on a integer variable ("2").

And in my twig it's:

{% for item in data %}

...

   {{ item.decoded.startDate }}

...

In Controller:

foreach ($data as $d) {
   $d->decoded = json_decode($d->getValue());
}

Can somebody help skipping the error when the value is integer or null?

cervico
  • 33
  • 3
  • Fix the error in the controller, don't use mixed value arrays. How to check for null is answered in several variations in [this question](https://stackoverflow.com/questions/3264889/how-to-check-for-null-in-twig). – Daniel W. Sep 20 '21 at 10:29
  • Thanks for answer. I do not understand what do you mean by "mixed value arrays"? What should be fixed? @DanielW. – cervico Sep 20 '21 at 10:31
  • Yeah, maybe not all items have decoded.startDate. Perhaps implement that kind of check before trying to print the value. – Aleksandar Jakovljevic Sep 20 '21 at 10:39
  • It seems some `item.decoded` are an `object` containing date information and some contain just the `integer` `2`. That's mixed array values. Make them all contain an object or null. – Daniel W. Sep 20 '21 at 10:47

1 Answers1

0

Try to dump item.decoded to see if startDate is available

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 08:56