Notice: Trying to get property 'nid' of non-object
You will need to check if the $node variable is actually an instance of node object.

if ($node instanceof \Drupal\node\NodeInterface) {
  // You can get nid and anything else you need from the node object.
  $nid = $node-&gt;id();
}


See

How do I get the current node ID?