实现搜索功能节点JS/MySQL [英] Implement Search Functionality Node JS/MySQL

查看:56
本文介绍了实现搜索功能节点JS/MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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

感谢您的帮助

推荐答案

{{id}} 的大部分出现在 #each 循环中,建议 idpokemon 对象的属性.同时,您的 getUser({{id}}) 在此循环之外使用,导致 undefined 传递给绑定变量 id.我认为您想将文本输入的值传递给您的 getUser 函数,因此您的形式参数名称例如搜索输入.您可以使用 {{input value=searchinput}}>Ember helper 然后 getUser({{searchinput}}) 在您的按钮中.还要注意 编码查询参数传递调用 AJAX 服务.

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.

这篇关于实现搜索功能节点JS/MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆