Adding autocomplete for text-field I tried to implement an autocomplete in text-field for drupal 8 in my custom module 

all I wanted was to fetch and display the likely title I typed via autocomplete
so declared a public function autocomplete within a class  in DefaultController.php 
in folder directory -->mymodule/src/Controller/DefaultController.php

&lt;?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;

class DefaultController extends ControllerBase
{
    public function autocomplete($string)
    {
        $matches = array();
        $db = \Drupal::database();
        $result = $db-&gt;select('node_field_data', 'n')
        -&gt;fields('n', array('title', 'nid'))
        -&gt;condition('title', '%'.db_like($string).'%', 'LIKE')
        -&gt;addTag('node_access')
        -&gt;execute();

        foreach ($result as $row) {
            $matches[$row-&gt;nid] = check_plain($row-&gt;title);
        }

        return new JsonResponse($matches);
    }
}


then created an EditForm.php in 
folder directory -->mymodule/src/Form/EditForm.php

&lt;?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class EditForm extends FormBase
{
    public function getFormId()
    {
        return 'mymodule_edit_form';
    }

    public function buildForm(array $form, FormStateInterface $form_state)
    {
        $form = array();

  $form['input_fields']['nid'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Name of the referenced node'),
    '#autocomplete_route_name' =&gt; 'mymodule.autocomplete',
    '#description' =&gt; t('Node Add/Edit type block'),
    '#default' =&gt; ($form_state-&gt;isValueEmpty('nid')) ? null : ($form_state-&gt;getValue('nid')),
    '#required' =&gt; true,
  );

        $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Create'),
  );

        return $form;
    }
}


also created mymodule.routing.yml

  mymodule.autocomplete:
  path: '/mymodule/autocomplete'
  defaults:
    _controller: '\Drupal\mymodule\Controller\DefaultController::autocomplete'
  requirements:
    _permission: 'access content'


still autocomplete functionality not getting implemented ?
Can anybody point me what am i missing ??
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.