在app.js里面 小程序一开始就初始化,多次调用只有第一次触发
onLaunch() {
console.log("小程序打开");
wx.cloud.init({
env: 'ayang-8g50ew302a3a6c5a', //云开发id
})
}
常规写法
wx.cloud.database().collection('goods').get({
//wx.cloud.database()是初始化函数,也可以定义一个实例来操作数据库
success: function (res) {
// res.data 包含该记录的数据
console.log("请求成功", res.data)
},
fail(err) {
console.log("请求失败", err)
}
})
es6写法(支持then)
wx.cloud.database().collection('goods').get().then(res => {
console.log(res.data);
}).catch(err => {
console.log(err);
})
传统写法中的this指向父亲函数不是window
箭头函数没有this,它的this继承于它创建的作用域下的this
wx.cloud.database().collection('goods').where({
name: '苹果'
}).get().then(res=>{})
wx.cloud.database().collection('goods').doc('f6e08a6462a55ee307a613306cd59003').get().then()
wx.cloud.database().collection('goods')
.add({
//添加数据
data: {
name: "香交",
price: '321'
}
}).then().catch()
wx.cloud.database().collection('goods').where({
_id: 'ca780ad562a9826e07511a73023c3b02'
})
.update({
//修改数据
data: {
name: "香交",
price: '32123123231'
}
}).then()
wx.cloud.database().collection('goods').where({
name: '香交'
})
.remove().then()
wx.cloud.database().collection('goods').orderBy('price','asc').get().then()
wx.cloud.database().collection('goods').orderBy('price','asc').limit(1).get().then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})
wx.cloud.database().collection('num')
.limit(2)//限制读两条数据
.skip(0)//从第0条以后的数据开始
.get().then(res => {
console.log(res)
}).catch(err => {
console.log(err)
})
let db=wx.cloud.database()
db.collection('goods').where({
price:db.command.gt(50)
}).get().then(res=>{
console.log(res)
})
let db=wx.cloud.database()
db.collection('goods').where({
price:db.command.lt(50)
}).get().then(res=>{
console.log(res)
})
let db = wx.cloud.database()
const _=db.command
db.collection('goods').where(
_.and([{
price: _.gt(20)
}, {
price: _.lt(40)
}])
).get().then(res => {
console.log(res)
})
(1)创建一个文件夹cloud与pages平齐


然后点击保存,我们的cloud文件夹前面就有一个云朵
