【问题标题】:Implement Search Functionality Node JS/MySQL实现搜索功能节点 JS/MySQL
【发布时间】:2017-11-22 16:08:32
【问题描述】:

在我的 Node JS/MySQL DB 上通过 Pokemon 名称正确实现搜索过滤器时遇到了一些麻烦。当我搜索时,我收到“search/?key=undefined 404 (Not Found)”有什么想法吗?这是我的搜索路线

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

这是我用来渲染搜索栏/搜索按钮的把手文件

<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>

最后是我在 JQuery 中的 search.js 函数

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

感谢您的帮助

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    {{id}} 的大多数出现在 #each 循环内,表明 id 是 pokemon 对象的属性。同时,您的getUser({{id}}) 在此循环之外使用,导致undefined 传递给绑定变量id。我认为您想将文本输入的值传递给您的 getUser 函数,因此您的形式参数的名称例如searchinput。您可以在按钮中使用 {{input value=searchinput}} 中的 Ember helper 和 getUser({{searchinput}})。还要注意 encoding query params 传递给被调用的 AJAX 服务。

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2021-01-08
      相关资源
      最近更新 更多