question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Callback not being called when typing

See original GitHub issue

trying to implement the typeahead search in nodejs with mysql, but looks like there is a bug:

When manually going to http://127.0.0.1:5000/search?key=s for example, I can see the result and my debug logs in the console. But when typing in the input text, it’s not showing anything nor can’t see the console.log(“debug”) in the console so looks like the /search is not even called

here is the index.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>test</title>
    <script src="https://code.jquery.com/jquery-latest.js"></script>
    <script src="https://cdn.rawgit.com/corejavascript/typeahead.js/master/dist/typeahead.jquery.js"></script>
    
</head>

<body>
    <input class="typeahead tt-query" spellcheck="false" autocomplete="off" name="typeahead" type="text" />
    <script type="text/javascript">
        $(document).ready(function(){
            $('input.typeahead').typeahead({
                name: 'Name',
                remote: 'http://127.0.0.1:5000/search?key=%QUERY',
                limit: 10
            });
        });
    </script>
</body>
</html>

and the App.js:

const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const path = require('path');
const app = express();

const {getHomePage} = require('./routes/index');
const port = 5000;

// create connection to database
// the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name.
const db = mysql.createConnection ({
    host: 'localhost',
    user: 'raid',
    password: 'raid',
    database: 'raid',
    port: 3308
});

// connect to database
db.connect((err) => {
    if (err) {
        throw err;
    }
    console.log('Connected to database');
});
global.db = db;



// configure middleware
app.use(express.static(__dirname + '/public'));
app.set('port', process.env.port || port); // set express to use this port
app.set('views', __dirname + '/views'); // set express to look in this folder to render our view
app.set('view engine', 'ejs'); // configure template engine
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // parse form data client
app.use(express.static(path.join(__dirname, 'public'))); // configure express to use public folder
app.use(fileUpload()); // configure fileupload

app.engine('html', require('ejs').renderFile)
// routes for the app

app.get('/raid', function(req, res){
    res.render('index.html');
})

app.get('/search', function(req, res){
    console.log("debug")
    db.query('SELECT Name from champions where Name like "%'+req.query.key+'%"',
    function(err, rows, fields) {
    if (err) throw err;
    var data=[];
    for(i=0;i<rows.length;i++)
    {
    data.push(rows[i].Name);
    }
    res.end(JSON.stringify(data));
    });
    });

app.get('/', getHomePage);


// set the app to listen on the port
app.listen(port, () => {
    console.log(`Server running on port: ${port}`);
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
senechalmcommented, Jun 23, 2020

Hi, thanks very much for your support, I now fixed the issue thanks to your help:

  1. Changed the backend to return the Name and ID : [{id: 1,Name:"Valerie"},{id:2,Name:"Chevalier Cerf"}]
  2. on the frontend, I’m using the Name for displaying purpose, but keep the ID to do other stuff

This can be closed

0reactions
jlbookercommented, Jun 23, 2020

Glad to hear you have it working!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Callback not being invoked - javascript - Stack Overflow
I create a function, move it to global scope, and then I add the querystring var to reference it, but it never gets...
Read more >
callback not being called · Issue #2568 - GitHub
The request function is asynchronous which means that it won't be called until the HTTP response has been received.
Read more >
JavaScript Callback Functions – What are Callbacks in JS and ...
As we can see, the callback function here has no name and a function definition without a name in JavaScript is called as...
Read more >
Callbacks / Callables - Manual - PHP
Callbacks registered with functions such as call_user_func() and call_user_func_array() will not be called if there is an uncaught exception thrown in a ...
Read more >
Advanced Callbacks | Dash for Python Documentation | Plotly
If a Dash app has multiple callbacks, the dash-renderer requests callbacks to be executed based on whether or not they can be immediately...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found