美文网首页
小程序云开发一些常用API

小程序云开发一些常用API

作者: SeanLink | 来源:发表于2021-07-02 17:52 被阅读0次

初始化时调用

 wx.cloud.init({
        // env 参数说明:
        //   env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境(第一个创建的环境)
        // env: 'my-env-id',
      //是否在将用户访问记录到用户管理中,在控制台中可见
        traceUser: true,
      })

获取集合对象

const dbhh = wx.cloud.database()
const  collection = dbhh.collection('students')

❌错误写法

collection.add({
      name:'lilei',
      age:28,
      height:1.88,
      friend:{
        name:'hanmeimei',
        age:18
      },
      location:dbhh.Geo.Point(100,50),
      brith:'1991-1-2',
    })

✅正确写法:

//向集合中添加一个对象
    collection.add({
      data:{
        name:'lilei',
        age:28,
        height:1.88,
        friend:{
          name:'hanmeimei',
          age:18
        },
        location:dbhh.Geo.Point(100,50),
        brith:'1991-1-2',
      }
    }).then(res => {
       console.log(res);
    }).catch(err=>{
      console.log('失败了啊')
      console.log(err)
    })```
更新集合中某个元素信息:

collection
.doc("28ee4e3e60de767c26f0922f1915b1c9")
.update({
data:{
age:28,
scroe:100
}
}).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})




删除集合中的对象:

collection.doc("28ee4e3e60de767c26f0922f1915b1c9")
.remove().then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})



查询操作人的数据

collection.where({
_openid:this.data.openid
}).get().then(res=>{
this.setData({
queryResult: JSON.stringify(res.data, null, 2)
})
}).catch(err=>{
console.log(err)
})

获取集合分页信息:
const MAX_LIMIT = 10
正常情况下位 skip(i * MAX_LIMIT).limit(MAX_LIMIT)

db.collection('todos')
.where({
_openid: 'xxx', // 填入当前用户 openid
})
.skip(10) // 跳过结果集中的前 10 条,从第 11 条开始返回
.limit(10) // 限制返回数量为 10 条
.get()
.then(res => {
console.log(res.data)
})
.catch(err => {
console.error(err)
})

条件查询:

//使用查询指令查询数据 gte(20) 大于20 . lt(20)小于20
const cmd = db.command;
collection.where({
age:cmd.gte(20)
}).get()
.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})

利用正则查询数据

//使用正则查询
collection.where({
name:db.RegExp({
regexp:"^li.*",
options:"i"
})
}).get()
.then(res=>{
console.log(res)
})



及时通讯api

//开启监听房间变化
db.collection("chatroom").where({
groupid:"110"
}).watch().then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})

//发送消息
db.collection("chatroom").add({
data:{
groupid:"110",
message:"吃了吗"
}
}).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})

相关文章

  • 小程序云开发一些常用API

    初始化时调用 获取集合对象 ❌错误写法 ✅正确写法: collection.doc("28ee4e3e60de76...

  • 微信小程序云开发-数据库知识点

    1,云开发数据库 API 分为小程序端和服务端两部分,小程序端 API 拥有严格的调用权限控制,开发者可在小程序内...

  • 通过HTTP API 方式去调用小程序的云开发

    HTTP API 提供了小程序外访问云开发资源的能力,,使用 HTTP API 开发者可在已有服务器上访问云资源,...

  • 小程序云开发 - 数据库

    云开发-数据库 数据库 API 分为小程序端和服务端两部分,小程序端 API 拥有严格的调用权限控制,开发者可在小...

  • 小程序Cloud开发归纳

    小程序云开发 小程序·云开发是微信团队联合腾讯云推出的专业的小程序开发服务。开发者可以使用云开发快速开发小程序、小...

  • WebSocket心跳重连讲解

    最近在开发小程序用到了WebSocket,小程序提供了相应的原生API,与H5的API使用方式上有一些区别,所以流...

  • 小程序外上传文件至云存储的实现

    在小程序云开发中,要实现上传文件至云存储,有两种方案:云函数和HTTP API,前者是在小程序内调用的,而后者则是...

  • 小程序云开发教程(初识篇)

    目录一、为什么学小程序云开发二、小程序云开发是什么三、学习准备 系列教程小程序云开发教程(入门篇)小程序云开发教程...

  • 开发入门

    相关文档 小程序开发API文档小程序开放工具文档小程序开发工具下载页面

  • 小程序封装request请求

    前言 小程序开发中都会调用后端工程师开发的API,小程序的开发文档提供了相对实用的API wx.request()...

网友评论

      本文标题:小程序云开发一些常用API

      本文链接:https://www.haomeiwen.com/subject/ybcsultx.html