Applet cloud database additions and deletions to change search

1 js add code to call the database page's initialization database

const db = wx.cloud.database();

If the test line 2 can be switched to select the test database environment

3 insert data

< Button bindtap = 'INSERT' > insert data </ Button >
  insert: function() {
    db.collection('user').add({
      data: {
        name: 'jack',
        age: 18
      }
    }).then(res => {
      console.log(res);
    }).catch(err => {
      console.log(err);
    })

  },

Click to insert test data will show success

4 Update Data

< Button bindtap = "Update" > Update Data </ Button >
 update: function() {
    db.collection('user').doc('90b4093b5d78b5bb17cf269a03a85679').update({
      data: {
        age: 21
      }
    }).then(res => {
      console.log(res);
    }).catch(err => {
      console.log(err);
    })
  },

5 Find Data

<Button bindtap = "search"> Finding Data </ ​​button>

  

  search: function() {
    db.collection('user').where({
      name: 'jerry'
    }).get().then(res => {
      console.log(res);
    }).catch(err => {
      console.log(err);
    })
  },

 

Guess you like

Origin www.cnblogs.com/polax/p/11613942.html