Twitter Typeahead.js in Node js NOT fetching suggestions

898 views Asked by At

CODE USED FOR TYPEAHEAD :

<script type="text/javascript">
  $('input.typeahead').typeahead({
    name: 'Artist',
    prefetch:{url: '/queryjson, ttl: '1'},
    template: '<p><strong>{{firstname}}</strong>',
    limit : 10,
    engine: Hogan,
  });
</script>

Code in App.js :

app.get('/queryjson', function(req,res,next){
  var firstname = req.body.firstname;
  connection.query("select firstname from entries",
  function (err, rows, fields) {
    if (err)
    throw err;
    res.end(JSON.stringify({
      data : rows
    }));
  });
})

AND FINALLY THE CODE FOR THE INPUT TEXT IN HTML :

<input class="typeahead" type="text" placeholder="Artist" data-provide="typeahead">

Note : When i type /queryjson in the address bar, the rows generated by the database are available, and in a json format ( {"data":[{"firstname":"sheila"},{"firstname":"Noreen"}... )

But when i type something inside the input text, no suggestions are generated.

Do you have any idea on what might be possible the issues? I really, really need your help.

Or do you have any suggestions on the proper implementation of typeahead in node using prefetch?

1

There are 1 answers

3
actual_kangaroo On

you are missing a single quote! replace '/queryjson, with '/queryjson',

appart from that, it looks correct, http://jsfiddle.net/8GJh2/

Edit: I also notice that your json format is different, they do not include the 'data' element

queryjson:

[
   { "firstname": "sheila"},
   { "firstname":"Noreen"}
]

App.js

res.end(rows);