Can't find the cause of this error
Line line 29:

$nid = $node->nid->value;

Try this solution:

$node-&gt;id();


Very useful thing "Drupal 8 Entity API cheat sheet": https://www.metaltoad.com/blog/drupal-8-entity-api-cheat-sheet