mysql - 如何使用 nodejs 和 ajax 使 typeahead 显示来自 mysql 的信息
问题描述
嗨,我正在尝试使用 typeahead 来使用 nodejs 和 express 显示本地数据库(Mysql)中的信息,但它不起作用
我已经尝试使用本教程https://codeforgeek.com/ajax-search-box-using-node-mysql/但它不起作用,我不知道我还能做什么
createSessions.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));
}
);
});
阿贾克斯
$(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>
我希望像这样工作,但显示来自我的数据库的信息
解决方案
您也可以使用以下查询进行节点 js + mysql 自动完成搜索
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));
});
推荐阅读
- android - CameraX chooses low resolution for given aspect ratio
- python - 我的身份验证代码中的变量似乎没有添加
- php - how to give only access to custom post type for a particular user
- java - In spring application, the first query (irrespective of any sql statement) is taking more than 12 secs to execute. Database is oracle
- javascript - 如何在字符串中使用正则表达式来选择这些字符
- javascript - How to process millions in a file inside Mongo shell with Javascript?
- angular - Angular FormArray validation error with ReactiveFormModule
- swift - Sort 'grouped' Firebase data in Swift
- php - 如何将字符串转换为只有双引号中的字符的数组
- javascript - Why is TinyMCE displaying HTML tags?