数据库查询
const db = uniCloud.database()
// 以user表为例
db.collection('user').get().then((res)=>{
// 数据对象数组res.result.data
}).catch((err)=>{
console.log(err.code)
console.log(err.message)
})
// 加上where
db.collection('user').where({name: '...'}).get().then((res)=>{
// 数据操作
// 如果只有一个数据,那么为res.result.data[0]
}).catch((err)=>{
console.log(err.code)
console.log(err.message)
})
这里面的this是可以指Vue实例的,所以是可以直接访问和修改data数据
数据库添加
const db = uniCloud.database()
db.collection('user').add({
name: this.username,
password: this.password,
}).then((res)=>{
// 数据操作
}).catch((err)=>{
console.log(err.code)
console.log(err.message)
})
数据库删除
const db = uniCloud.database()
db.collection('user').where({
name: username
}).remove().then((res)=>{
uni.showToast({
title: '用户删除成功',
icon: 'success'
})
}).catch((err)=>{
console.log(err.code)
console.log(err.message)
})
数据库更新
const db = uniCloud.database()
db.collection('user').where({
name: username
}).update({
age: 20
}).then((res)=>{
if (res > 0) {
// 数据更新成功
}
}).catch((err)=>{
console.log(err.code)
console.log(err.message)
})