小程序操作云数据库

2019-07-26  本文已影响0人  lydia56

1.初使化数据库

const db = wx.cloud.database(); 
//切换环境
const testDB = wx.cloud.database({
        env: 'test'
});

2.插入数据

//回调函数的写法
db.collection(‘collectionName').add({
        data:{
                id: this.data.id,
                value:this.data.value
        },
        success: res => {
              console.log(res);
        },
        fail: err => {
                console.log(err);
        }
})

//promise的写法
db.collection(‘collectionName').add({
        data:{
                id: this.data.id,
                value:this.data.value
        }
}).then(res=>{
         console.log(res);
}).catch(err=>{
        console.log(err);
})

3.通过_id更新数据

db.collection('collectionName').doc('1a2b3c4d...').update({
        data:{
                value: 21
        }
}).then(res=>{
}).catch(err=>{
})

4.查询数据

 db.collection('collectionName').where({
        name:'jerry'
}).get().then(res=>{
}).catch(err=>{
})

5.通过_id删除数据

db.collection('collectionName').doc('1a2b3c4d...').remove()
.then(res=>{
}).catch(err=>{
})
上一篇 下一篇

猜你喜欢

热点阅读