Adding autocomplete for text-field
If you want to select an entity, then there is a way easier way to do that. Drupal 8 has a standard entity_autocomplete field type, just specify your form element like this:

$form['node'] = [
  '#type' =&gt; 'entity_autocomplete',
  '#target_type' =&gt; 'node',
];


See Custom autocomplete field for more information.

Also, never do database queries against node/entity tables. Use \Drupal::entityQuery() for that.