mysqlnode.jsajaxexpresstypeahead.js

How to make that typeahead shows info from mysql using nodejs and ajax


Hi I'm trying to use typeahead to show the info from my local DB (Mysql) using nodejs and express but it's not working

I already tried using this tutorial https://codeforgeek.com/ajax-search-box-using-node-mysql/ but it's not working and I don't know what else i can do

createSesions.js

router.get('/add/search', isLoggedIn, (req, res) =>
{
    dbConnection.query('SELECT fullname FROM pacientes WHERE fullname LIKE ?"%' + req.query.key + '%"',
        (err, rows, fields) =>
        {
            if (err) throw err;
            var data = [];
            for (i = 0; i < rows.length; i++)
            {
                data.push(rows[i].fullname);
            }
            res.end(JSON.stringify(data));
        }
    );
});

Ajax

$(document).ready(function ()
{
    $('input.typeahead').typeahead(
    {
        name: 'paciente',
        remote: 'http://localhost:3000/createSesiones/add/search?key=%QUERY',
        limit: 10
    });
});

The Input

<div class="form-group">
  <input type="text" class="form-control typeahead tt-query" name="paciente"
     placeholder="Asignar paciente" autofocus required>
</div>

I expect to work like this but showing the info from my db

https://twitter.github.io/typeahead.js/


Solution

  • Ok I solved it by removing the "?"

    from this:

    dbConnection.query('SELECT fullname FROM pacientes WHERE fullname LIKE ?"%' + req.query.key + '%"',
    

    To this:

    dbConnection.query('SELECT fullname FROM pacientes WHERE fullname LIKE "%' + req.query.key + '%"',
    

    and updating jquery