1. Add data
Based on the previous tables and fields, we will now add data on the applet side.
1). On the home page, we first write the new data interface and add the following code in /routes/ animos.js. New data is generally requested by POST. Since asynchronous operations are involved, async, await operations are used. I didn’t upload a file, so I’ll just write cover_url. The image is placed under /public/images.
.const {mysql, mongodb} = require('access-db') // Import mysql operations, or mongodb operations.// Use post here
routerAnime.post('/add'.async function(req, res, next) {
const {title, total, type} = req.body // Data uploaded by the applet side
try{
let tempRes = await mysql.set('anime', {
title: title,
cover_url: 'http://localhost:3000/images/1.webp'.total: total,
type: type,
follows: 23
})
res.json(tempRes.data.insertId)
}catch(err) {
console.log('errrr', err)
}
});
Copy the code
The req.body is the argument that the POST request takes. The mysql.set method is used to add data. The first parameter is the name of the table, and the second parameter is the content to be added. The field name must be the same as the field name in the data table. The interface address is: http://localhost:3000/anime/add after modifying code, remember to restart the back-end services
2). Add a binding event addOneData in the wechat applet
index.wxml
<! --index.wxml-->
<view class="container">
<button bindtap="addOneData">The new data</button>
</view>
Copy the code
index.js
.addOneData(){
wx.request({
url: 'http://localhost:3000/anime/add'.method: 'POST'.data: {
title: Ghostblaster Blade.total: 26.type: 1
},
success: (res) = > {
console.log('res', res)
}
})
}
...
Copy the code
Finally, click the button to add data successfully. Note to turn on the setting of not verifying the domain name.
2. Obtain a piece of data
Through ID, we can directly obtain the details of a certain data and obtain data. Use get to add a get request to /router/ animo.js. The parameters of the GET request are in req.params.
// Use get to request
routerAnime.get('/detail/:id'.async function(req, res, next){
const {id} = req.params
let temp = await mysql.get('anime', id)
res.json(temp.data) // Return data as json
})
Copy the code
After writing the above code, remember to restart the server.
And then, on the applet side, we also bind an event, getOneData
.getOneData(){
wx.request({
url: 'http://localhost:3000/anime/detail/' + 5.method: 'GET'.success: (res) = > {
console.log('Get a piece of data:', res.data)
}
})
},
...
Copy the code
As you can see, getting data with ID 5 returns success
3. Update data
In the same way, /routes/ animos.js is added as follows. When you update, you also want to know which data you’re updating, so there’s a lot of ids. Update data, usually with the PUT method. As follows:
routerAnime.put('/update/:id'.async function(req, res, next){
const {id} = req.params // The argument that follows the url
const {title} = req.body // The parameters of data in the applet request
let temp = await mysql.update('anime', id, {
title: title
})
res.json(temp.data.changedRows)
})
Copy the code
Restart the background server. And then, on the applet side, we’ll also bind an event, updateOneData
.updateOneData(){
wx.request({
url: 'http://localhost:3000/anime/update/' + 5.method: 'PUT'.data: {
title: 'New Heading 3'
},
success: (res) = > {
console.log('Update data:', res.data)
}
})
...
Copy the code
You can see that the title of the data with ID 5 was successfully updated.
4. Search for data
/routes/ animos.js, add as follows. Search generally use get method. P0 is a single search condition, an array of parameters, the first being a field, the second a condition, and the third a content. R is the rule to perform the search. If we were looking for a value of type equal to 1, we could write it as follows
routerAnime.get('/list/:page'.async function(req, res, next){
const {page} = req.params
let temp = await mysql.find('anime', {
p0: ['type'.'='.1].r: 'p0'
})
res.json(temp.data.objects)
})
Copy the code
Restart the server, and on the applet side, add the event getList
getList(){
wx.request({
url: 'http://localhost:3000/anime/list/' + 1.method: 'GET'.success: (res) = > {
console.log('Search result:', res.data)
}
})
},
Copy the code
Well, here, basically, you can start to write their own small program, or very simple, ha ha ~