1

Having some trouble properly implementing a search filter by the Pokemon Name on my Node JS/MySQL DB. When I search I am getting "search/?key=undefined 404 (Not Found)" Any ideas? Here is my search route

 router.get('/search', function(req, res){
        var mysql =  req.app.get('mysql');
        var sql='SELECT pokemonname FROM pokemon WHERE pokemonname LIKE "%' + req.key.query + '%';
        sql = mysql.pool.query(sql, function(error, results, fields) {
                if(error) {
                   res.write(JSON.stringify(error));
                   res.end();
                } else {
                        res.status(200);
                        res.end();
        }
   });
});

Here is my handlebars file to render the search bar/search button

<table id="table">
    <thead>
        <th>Pokemon Name   </th>
        <th>Evolution Level   </th>
        <th>Move Name   </th>
    </thead>
    <input type="text" class="search form-control" id="searchinput" placeholder="Pokemon Name">
    <input type="button" class="btn btn-primary" value="Search" onclick="getUsers({{id}})"/>
        <br>
    <tbody>
        {{#each pokemon}}
        <tr>
            <td>{{pokemonname}}</td>
            <td>{{evolutionlevel}}</td>
            <td>{{movename}}</td>
            <td><button onclick="deletePokemon({{id}})">Delete</td>
        <td><a href="/pokemon/{{id}}">Update</a></td>
        </tr>
        {{/each}}
    </tbody>

And finally my search.js function in JQuery

function getUsers(searchinput){
        $.ajax({
                type: 'GET',
                url: '/search/?key=' + searchinput,
                success: function(result){
                        window.location.reload(true);
                }
        })
};

Thanks for the help

user8991794
  • 45
  • 3
  • 8

1 Answers1

0

Most occurences of {{id}} are used inside #each loop suggesting id is property of pokemon object. Meanwhile your getUser({{id}}) is used outside of this loop leading to undefined passed to binding variable id. I think that you want to pass value of text input to your getUser function instead, hence your name of formal parameter e.g. searchinput. You could use {{input value=searchinput}} from Ember helper and then getUser({{searchinput}}) in your button. Also pay attenttion to encoding query params passed to called AJAX service.

andy
  • 757
  • 5
  • 13