如何使 typeahead 显示来自 mysql 使用 nodejs 和 ajax 的信息

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

您好,我正在尝试使用 nodejs 和 express 使用 typeahead 来显示来自我的本地数据库 (Mysql) 的信息,但它不起作用

我已经尝试使用本教程https://codeforgeek.com/ajax-search-box-using-node-mysql/,但它不起作用,我不知道我还能做什么

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
    });
});

输入

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

我希望像这样工作,但显示来自我的数据库的信息

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

好的,我通过删除“?”解决了这个问题。

来自这个:

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

为此:

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

和更新jquery

您还可以使用以下查询 node js + mysql autocomplete search

    db.query('SELECT country_name FROM countries WHERE country_name LIKE "%' + req.query.term + '%"',
    function(err, rows, fields) {
        if (err) throw err;
        var data = [];
        for (i = 0; i < rows.length; i++) {
            data.push(rows[i].country_name);
        }
        res.end(JSON.stringify(data));
    });