当前位置:网站首页>Implementing MySQL fuzzy search with node and express
Implementing MySQL fuzzy search with node and express
2022-06-11 01:22:00 【Hehe cow】
node and express Realization mySql Fuzzy search
As a front-end Developer What are the disadvantages of missing the back-end So I am also learning express On the way .
1. Realization way
The search of the server is mainly realized by calling mySql sentence , To query the desired data in the database .
Ideas :
Parameters passed from the front end to the back end
Back end get parameters
Get the parameters and splice them into usable ones mysql sentence
express Return the response result to the front end
Specifically, the above four steps , How to achieve it ? Step by step .
1. Parameters passed from the front end to the back end

ad locum this.queryInfo The parameters that the front end needs to pass to the back end .
2. Back end get parameters

The red box shows the parameters that the back end gets from the front end
3. Get the parameters and splice them into usable ones mysql sentence

When you get the parameters Get all the parameters Spliced into the final sql sentence below sql You can get Telephone number band 11 All mobile phone numbers .
select * from userlist where phone like "%11%" sql

4.express Return the response result

express Through the callback Return the query results to the front end for use

mysql Return to express The data of express Return to the front end through callback , The front end gets this data to render the table .
Finally, post all the codes I hope it works for you
Server code
var express = require("express");
var router = express.Router();
var sqlMap = require('../../config/sqlMap');
var conn = require('../../config/db');
var verify = require('../../utils/utils');
router.post('/all', function (req, res) {
if (req.header.token || verify(req.headers.token).code === '606') {
res.json({
code: 401,
message: ' Login information has expired , Please login again '
})
return
}
// let sql = sqlMap.item.findAllInfo;
let sql = 'select * from userlist ';
var params = req.body
console.log(params, ' Parameters ')
let arr = []
for (var key in params) {
arr.push(params[key])
}
console.log(arr, '000')
if (params.username !== '') {
if (arr[0] !== '') {
sql += `where username like "%${params.username}%" `;
} else {
sql += '';
}
}
if (params.phone !== '') {
if (arr[0] === '') {
sql += ` where phone like "%${params.phone}%" `;
} else {
sql += ` and phone like "%${params.phone}%" `;
}
}
if (params.account !== '') {
if (arr[0] === '' && arr[1] === '') {
sql += ` where account like "%${params.account}%" `;
} else {
sql += ` and account like "%${params.account}%" `;
}
}
console.log(sql, 'sql')
conn.query(sql, (err, result) => {
if (err) {
console.log(err)
}
if (result) {
res.json({
code: 200,
data: result
})
}
})
})
module.exports = router;// Expose this route
Client code
<template>
<div>
<el-card>
<el-form
:inline="true"
:model="queryInfo"
ref="queryInfoRef"
class="demo-form-inline"
>
<el-form-item label=" user name ">
<el-input
v-model="queryInfo.username"
placeholder=" user name "
></el-input>
</el-form-item>
<el-form-item label=" Telephone ">
<el-input v-model="queryInfo.phone" placeholder=" Telephone "></el-input>
</el-form-item>
<el-form-item label=" type ">
<el-select v-model="queryInfo.account" placeholder=" type ">
<el-option label="admin" value="admin"></el-option>
<el-option label="admin1" value="admin1"></el-option>
<el-option label="admin2" value="admin2"></el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSearch"> Inquire about </el-button>
<el-button @click="onReset('queryInfoRef')"> Reset </el-button>
</el-form-item>
</el-form>
<el-table :data="userList" border style="width: 100%">
<el-table-column fixed prop="username" label=" user name " min-width="150">
</el-table-column>
<el-table-column prop="phone" label=" Telephone " min-width="120">
</el-table-column>
<el-table-column prop="account" label="account" min-width="120">
</el-table-column>
<el-table-column fixed="right" label=" operation " min-width="100">
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small"
> see </el-button
>
<el-button type="text" size="small"> edit </el-button>
</template>
</el-table-column>
</el-table>
<el-pagination layout="prev, pager, next" :total="this.userList.length"> </el-pagination>
</el-card>
</div>
</template>
<script>
export default {
data() {
return {
userList: [],
queryInfo: {
username: "",
phone: "",
account: ""
}
};
},
mounted() {
this.getList()
},
methods: {
getList(){
this.$ajax
.post("/item/all",this.queryInfo)
.then(res => {
this.userList = res.data;
})
.catch(err => {
console.log(err);
});
},
onSearch() {
this.getList()
console.log(this.queryInfo, "000");
},
onReset(resetName) {
for (var key in this.queryInfo) {
this.queryInfo[key] = "";
this.getList()
}
}
}
};
</script>
边栏推荐
- 用data和proc怎么写出这个,不用sql
- 焱融看|混合云环境下,如何实现数据湖最优存储解决方案
- Viewpager and dot of bottom wireless loop
- Deepstream series fish eye camera test
- SAS判别分析(Bayes准则和proc discrim过程)
- 明朝的那些皇帝
- Introduction to the application process of China Patent Award, with a subsidy of 1million yuan
- 北京中国专利奖申报流程介绍,补贴100万
- [paper translation] recent advances in open set recognition: a survey
- 中间件_Redis_06_Redis的事务
猜你喜欢

明朝的那些皇帝

Current limiting and download interface request number control

IRS application release 16: H5 application design guide

多兴趣召回模型实践|得物技术

Why can't Google search page infinite?
![[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism](/img/ad/d798284ceb370f7c68cbab755a11de.png)
[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism

最好的創意鼓工具:Groove Agent 5
[ROS tutorial] - 02 ROS installation

Centos7 actual deployment mysql8 (binary mode)

IRS应用发布之十六:H5 应用设计指南
随机推荐
IRS应用发布之十六:H5 应用设计指南
Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
2022 recognition requirements for new technologies and new products (services) in Huairou District, Beijing
ion_dma_buf_begin_cpu_access
What is the C-end and what is the b-end? Let me tell you
什么是C端 什么是B端 这里告诉你
[introduction to ROS] - 03 ROS workspace and function pack
After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?
Viewpager and dot of bottom wireless loop
快递鸟系统对接
深圳市南山区专精特新企业申报流程,补贴10-50万
北京中国专利奖申报流程介绍,补贴100万
SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)
[paper reading] fixmatch: simplifying semi supervised learning with consistency and confidence
Promise
Projet Visualisation et analyse des données sur les épidémies basées sur le Web crawler
Recruitment | Nanjing | triostudio Sanli Agency - Interior Designer / construction drawing deepening Designer / device / Product Designer / Intern, etc
Time dependent - format, operation, comparison, conversion
Non presented paper (no show) policy
Array simulation [queue] and [ring queue]_ code implementation