微信小程序云開(kāi)發(fā)之?dāng)?shù)據(jù)庫(kù)操作
本文實(shí)例為大家分享了微信小程序云開(kāi)發(fā)之?dāng)?shù)據(jù)庫(kù)操作的具體代碼,供大家參考,具體內(nèi)容如下
新建集合
1.打開(kāi)云開(kāi)發(fā)控制臺(tái),數(shù)據(jù)庫(kù)
2.添加集合users
添加代碼
onAdd: function () {
const db = wx.cloud.database()
db.collection('users').add({
data: {
count: 1
},
success: res => {
// 在返回結(jié)果中會(huì)包含新創(chuàng)建的記錄的 _id
this.setData({
counterId: res._id,
count: 1
})
wx.showToast({
title: '新增記錄成功',
})
console.log('[數(shù)據(jù)庫(kù)] [新增記錄](méi) 成功,記錄 _id: ', res._id)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '新增記錄失敗'
})
console.error('[數(shù)據(jù)庫(kù)] [新增記錄](méi) 失?。?, err)
}
})
},

查詢記錄
onQuery: function() {
const db = wx.cloud.database()
// 查詢當(dāng)前用戶所有的 counters
db.collection('users').where({
_openid: this.data.openid
}).get({
success: res => {
console.log(res);
this.setData({
queryResult: JSON.stringify(res.data, null, 2)
})
console.log('[數(shù)據(jù)庫(kù)] [查詢記錄](méi) 成功: ', res)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '查詢記錄失敗'
})
console.error('[數(shù)據(jù)庫(kù)] [查詢記錄](méi) 失?。?, err)
}
})
},

更新記錄
onCounterInc: function() {
const db = wx.cloud.database()
const newCount = this.data.count + 1
db.collection('users').doc(this.data.counterId).update({
data: {
count: newCount
},
success: res => {
console.log(res);
this.setData({
count: newCount
})
},
fail: err => {
icon: 'none',
console.error('[數(shù)據(jù)庫(kù)] [更新記錄](méi) 失敗:', err)
}
})
},
onCounterDec: function() {
const db = wx.cloud.database()
const newCount = this.data.count - 1
db.collection('users').doc(this.data.counterId).update({
data: {
count: newCount
},
success: res => {
this.setData({
count: newCount
})
},
fail: err => {
icon: 'none',
console.error('[數(shù)據(jù)庫(kù)] [更新記錄](méi) 失?。?, err)
}
})
},

刪除記錄
if (this.data.counterId) {
const db = wx.cloud.database()
db.collection('users').doc(this.data.counterId).remove({
success: res => {
wx.showToast({
title: '刪除成功',
})
this.setData({
counterId: '',
count: null,
})
},
fail: err => {
wx.showToast({
icon: 'none',
title: '刪除失敗',
})
console.error('[數(shù)據(jù)庫(kù)] [刪除記錄](méi) 失?。?, err)
}
})
} else {
wx.showToast({
title: '無(wú)記錄可刪,請(qǐng)見(jiàn)創(chuàng)建一個(gè)記錄',
})
}
這個(gè)官方的demo做的可以,通俗易懂
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序?qū)崿F(xiàn)簡(jiǎn)單的select下拉框
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)單的select下拉框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
詳解JavaScript數(shù)組和字符串中去除重復(fù)值的方法
這篇文章主要介紹了詳解JavaScript數(shù)組和字符串中去除重復(fù)值的方法,及利用各種限制條件對(duì)數(shù)組和字符串進(jìn)行過(guò)濾,需要的朋友可以參考下2016-03-03
js實(shí)現(xiàn)單層數(shù)組轉(zhuǎn)多層樹(shù)
這篇文章主要介紹了js實(shí)現(xiàn)單層數(shù)組轉(zhuǎn)多層樹(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Javascript日期格式化format函數(shù)的使用方法
這篇文章主要介紹的是javascript時(shí)間格式format函數(shù),我們有時(shí)候調(diào)用的new Date()不是格式化的時(shí)間,可能顯示不是很正常,那么這時(shí)候就需要格式化時(shí)間,今天這里分享一個(gè)javascript的foramt()函數(shù),有需要的可以參考借鑒。2016-08-08
學(xué)習(xí)JavaScript事件流和事件處理程序
這篇文章主要為大家介紹了學(xué)習(xí)JavaScript事件流和事件處理程序的注意事項(xiàng),感興趣的小伙伴們可以參考一下2016-01-01
javascript 拷貝節(jié)點(diǎn)cloneNode()使用介紹
這篇文章主要介紹了javascript 節(jié)點(diǎn)操作拷貝節(jié)點(diǎn)cloneNode()的使用,需要的朋友可以參考下2014-04-04
JS遍歷Json字符串中鍵值對(duì)先轉(zhuǎn)成JSON對(duì)象再遍歷
這篇文章主要介紹了JS遍歷Json字符串中鍵值對(duì)的方法,先將Json字符串轉(zhuǎn)換成JSON對(duì)象,再進(jìn)行遍歷,需要的朋友可以參考下2014-08-08

