1.新增數(shù)據(jù)
根據(jù)前面的表和字段禁灼,現(xiàn)在我們要在小程序端進(jìn)行新增數(shù)據(jù)操作荷荤。
1). 首頁缓熟,我們先寫新增數(shù)據(jù)接口,在/routes/anime.js
加入以下代碼走诞。新增數(shù)據(jù)一般用post
請求副女。因為涉及到異步操作,所以用async
蚣旱、await
進(jìn)行操作碑幅。因為沒做文件上傳,所以姻锁,cover_url我就直接寫了枕赵。圖片是放在/public/images
下面的。
...
const {mysql, mongodb} = require('access-db') //引入mysql操作方法位隶,如果是mongodb拷窜,就引入對應(yīng)的
...
//這里用post
routerAnime.post('/add', async function(req, res, next) {
const {title, total, type} = req.body //小程序端上傳的數(shù)據(jù)
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)
}
});
req.body
就是post請求時,所帶的參數(shù)涧黄。mysql.set
方法篮昧,就是新增數(shù)據(jù),第一個參數(shù)就是數(shù)據(jù)表表名笋妥,第二個參數(shù)懊昨,就是要新增的內(nèi)容。字段名必須和數(shù)據(jù)表里面字段名一致春宣。此時的接口地址為:http://localhost:3000/anime/add 修改代碼之后酵颁,記得重新啟動后臺服務(wù)
2). 在微信小程序里面,添加一個綁定事件addOneData
index.wxml
<!--index.wxml-->
<view class="container">
<button bindtap="addOneData">新增數(shù)據(jù)</button>
</view>
index.js
...
addOneData(){
wx.request({
url: 'http://localhost:3000/anime/add',
method: 'POST',
data: {
title: '鬼滅之刃',
total: 26,
type: 1
},
success: (res) => {
console.log('res', res)
}
})
}
...
最后月帝,點擊按鈕躏惋,即可成功添加數(shù)據(jù)了,注意打開不校驗域名的設(shè)置嚷辅。
2.獲取一條數(shù)據(jù)
通過id簿姨,我們可以直接獲取某條數(shù)據(jù)的詳情,獲取數(shù)據(jù)。一般用get
在/router/anime.js
里扁位,加入一個get請求准潭。get
請求的參數(shù),是在req.params
里域仇。
//用 get請求
routerAnime.get('/detail/:id', async function(req, res, next){
const {id} = req.params
let temp = await mysql.get('anime', id)
res.json(temp.data) // 以json類型返回數(shù)據(jù)
})
寫完上面代碼后刑然,記得重啟服務(wù)器。
然后殉簸,在小程序端闰集,我們同樣再綁定個事件,getOneData
...
getOneData(){
wx.request({
url: 'http://localhost:3000/anime/detail/' + 5,
method: 'GET',
success: (res) => {
console.log('獲取一條數(shù)據(jù):', res.data)
}
})
},
...
可以看到般卑,獲取id為5的數(shù)據(jù)返回成功
3.更新數(shù)據(jù)
同樣的道理武鲁,/routes/anime.js
,添加如下蝠检。更新時沐鼠,也要知道更新的是哪條數(shù)據(jù),所以叹谁,id也不少饲梭。更新數(shù)據(jù),一般用put
方法焰檩。如下:
routerAnime.put('/update/:id', async function(req, res, next){
const {id} = req.params //url后面跟的參數(shù)
const {title} = req.body //小程序請求里憔涉,data里的參數(shù)
let temp = await mysql.update('anime', id, {
title: title
})
res.json(temp.data.changedRows)
})
重啟后臺服務(wù)器;然后析苫,在小程序端兜叨,我們同樣再綁定個事件,updateOneData
...
updateOneData(){
wx.request({
url: 'http://localhost:3000/anime/update/' + 5,
method: 'PUT',
data: {
title: '新標(biāo)題3'
},
success: (res) => {
console.log('更新數(shù)據(jù):', res.data)
}
})
...
可以看到衩侥,成功更新的id為5的數(shù)據(jù)的title国旷。
4.查尋數(shù)據(jù)
/routes/anime.js
,添加如下茫死。查尋一般用get
方法跪但。
p0
為單個查尋條件,參數(shù)數(shù)組峦萎,第一個為字段屡久,第二個為條件,第三個為內(nèi)容爱榔。r
為執(zhí)行查尋的規(guī)則涂身。
假如我們要查尋type
值等于1的數(shù)據(jù),那么就可以如下這么寫
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)
})
重啟服務(wù)器搓蚪,然后在小程序端,新增事件getList
getList(){
wx.request({
url: 'http://localhost:3000/anime/list/' + 1,
method: 'GET',
success: (res) => {
console.log('查尋結(jié)果:', res.data)
}
})
},
好了丁鹉,到這里妒潭,基本上悴能,你就可以開始寫自己的小程序了,還是很簡單吧雳灾,哈哈~