当前位置:网站首页>Implementing MySQL fuzzy search with node and express
Implementing MySQL fuzzy search with node and express
2022-06-23 19:58:00 【Hehe cow】
This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together .
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>
边栏推荐
- Virtp4 notes
- 教你如何用网页开发桌面应用
- JDBC 在性能测试中的应用
- Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
- Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
- Development of block hash quiz game system (DAPP)
- Application of MySQL time function, simple problem
- Naacl 2022 finds | byte proposes MTG: multilingual text generation data set
- Rendering of kotlin jetpack compose tab using animatedvisibility
- Tcp/udp Fundamentals
猜你喜欢

The evolution of the "Rainbow Bridge" middleware platform for the acquisition database based on shardingsphere

混沌工程,了解一下

基于 ShardingSphere 的得物数据库中间件平台“彩虹桥”演进之路

Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage

How to write a great online user manual in 7 steps

NAACL 2022 Findings | 字节提出MTG:多语言文本生成数据集

Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)

好用的人事管理软件有哪些?人事管理系统软件排名!
随机推荐
Ugeek's theory 𞓜 application and design of observable hyperfusion storage system
测试的重要性及目的
LeetCode 473. Match to square
Save: software analysis, verification and test platform
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
20省市公布元宇宙路线图
Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface
Kubernetes resource topology aware scheduling optimization
Application of JDBC in performance test
How to write a great online user manual in 7 steps
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔
教你如何用网页开发APP
After the collapse of UST, will the stable currency market pattern usher in new opportunities?
20 provinces and cities announce the road map of the meta universe
Robust extraction of specific signals with time structure (Part 2)
80% of people will be wrong about the three counter intuitive questions?
如何使用物联网低代码平台进行流程管理?
Development of block hash quiz game system (DAPP)
NAACL 2022 Findings | 字节提出MTG:多语言文本生成数据集