WeChat small program cloud development database operation

Example of this article shared the specific code of database operations for WeChat small program cloud development, for all reference, specific content

New set

1. Open the cloud development console, database

add code

 onadd: function () {const db = wx.cloud.Database () db.collection ('users'). add ({data: {count: 1}, success: res => {// will contain newly created Record_id this.setdata ({counter: res._id, count: 1}) wx.showtoast ({Title: 'New Record Success',}) console.log (' [Database] [Add Record] Success , Record_ID: ', Res._ID)}, fail: err => {wx.showtoast ({icon:' none ', Title:' New record failed '}) console.Error (' [Database] [new Record] Failure: ', Err)}}}}}}}},   
Query recording


ONQUERY: FUNCTION () {const DB = wx.cloud.Database () // Query all of the current users all Counters db.collection (‘users’). Where ({_OpenID: this.Data.OpenID}). Get ({success); this.SetData ({queryResult: json.stringify (res. Data, Null, 2)}) Console.log (‘[Database] [Query Record] Success:’, RES)}, Fail: Err => {wx.ShowToast ({icon: ‘None’, Title: ‘Query Record Failed’}) Console.Error (‘[Database ] [Query Record] Failure: ‘, Err)}}}}},
   Update Record 

微信小程序云开发之数据库操作 oncounterinc: function () {const db = wx.cloud.database () const newcount = this.data.count + 1 db.collection (‘users’). Doc (this.data.counterid) .UPDATE ({Data: {Count: res => {console.log (res); this.SetData ({Count: newcount})}, fail: err => {icon: ‘None’, Console (“[Database] [Update Record] Failure: ‘, 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 (‘[database] [update record] failed:’, err)}}}}

Delete Record
IF (this.data.counterid) {Const DB = wx.Cloud.Database () db.collection (‘users’). DOC (THIS.DATA.COUNTERID) .remove ({Success: res => {wx.showtoast ({Title:’ Delete Success “,}) This.SetData ({Counterid: ”, count: null,})}, fail: err => {wx.showtoast ({icon: ‘none’, title: ‘Delete failed “}) console. error (” Database] [Delete Record] Failed: ‘, Err)}}} Else {wx.showtoast ({Title:’ No record delete, see Creating a record’,})}

This official Demo can do, it is easy to understand 微信小程序云开发之数据库操作

The above is all the content of this article, I hope to learn from everyone Helped, I also hope that everyone will support Tumi Clouds.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment