小程序云开发

问题解决

关于构建npm找不到包的解决办法:

  1. 进入小程序根目录,打开cmd,输入:npm init
  2. 输入命令之后一直点回车
  3. 输入命令:npm i miniprogram-sm-crypto --production
  4. 执行命令完之后,然后再去微信开发者中点工具-构建npm,然后就成功了

新增一条数据

  • 直接添加
const db = wx.cloud.database();
const dbCollections = db.collection('products')
add(){
      dbCollections.add({
         data:{
           title:"我是标题",
           image:"我是图片",
           tags:[1,2,3],
           price:111,
           color:'red'
         }
       }).then(res=>{
         console.log(res)
       })
  }
  • 云函数添加
    • 在cloudfunctions目录下新建addData云函数,在建好的index.js里面,添加如下代码:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const dbCollections = db.collection('products')
// 云函数入口函数
exports.main = async (event, context) => {
  return await dbCollections.add({
    data: {
      title: "我是标题云函数",
      image: "我是图片云函数",
      tags: [1, 2, 3],
      price: 111,
      color: 'red'
    }
  })
}
  • 在page里面的js文件调用(/page/add/add)
const db = wx.cloud.database();
const dbCollections = db.collection('products')
Page({
  add(){
    wx.cloud.callFunction({
      name:'addData'
    }).then(res=>{
      console.log(res)
    })
  }
})

查询所有数据

const db = wx.cloud.database();
const dbCollections = db.collection('products')
Page({
  onPullDownRefresh(){
  	//下拉
    dbCollections.get().then(res => {
      console.log(res)
    })
  },
  onReachBottom(){
    //上拉
  },
  list(){
    dbCollections.get().then(res=>{
      console.log(res)
    })
  }
})

分页查询

const db = wx.cloud.database();
const dbCollections = db.collection('products')
var start = 0;
Page({
  onReachBottom(){
    start+=10;
    //上拉
    dbCollections.skip(start).get().then(res => {
      console.log(res)
    })
  },
})

更新每一条数据统计量


筛选查询

const db = wx.cloud.database();
const dbCollections = db.collection('products')
const _ = db.command;

//简单筛选
dbCollections.where({
  color:'red'
}).get().then(res => {
  console.log(res)
})

//查询少于某个数
dbCollections.where({
  price:_.lt(50)
}).get().then(res => {
  console.log(res)
})

// 查询某个值在其中
dbCollections.where({
  price:_.in([50,51,111])
}).get().then(res => {
  console.log(res)
})

//查询某个值在某区间,大于20小于50
dbCollections.where({
  price: _.gt(20).and( _.lt(50))
}).get().then(res => {
  console.log(res)
})

//limit限制
dbCollections.limit(1).where({
  price: _.gt(10).and( _.lt(100))
}).get().then(res => {
  console.log(res)
})

排序查询

dbCollections
	.orderBy('price','asc')
	.orderBy('order','desc')
	.get().then(res => {
	  	console.log(res)
})

计数

dbCollections
.count()
.then(res => {
  console.log(res)
})

数据筛选

dbCollections
.field(
  {
    price:true
  }
)
.get()
.then(res => {
  console.log(res)
})

数据更新(批量更新)

// 在cloudFunctions目录下新建branceUpdate函数,代码如下:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const dbCollections = db.collection('products')
// 云函数入口函数
exports.main = async (event, context) => {
  console.log(context)
  return await dbCollections
  .where({
    color:event.color
  })
  .update({
    data: {
      title: "我是修改标题云函数",
      image: "我是修改图片云函数",
      tags: [1, 2, 3],
      price: 111,
      color: 'red'
    }
  })
}
// 代码调用
wx.cloud.callFunction({
  name: 'branchUpdate',
  data:{
    color:'blue'
  }
}).then(res => {
  console.log(res)
})

数据删除(单条数据)

dbCollections.doc('38034ae05dd67db201e9ee75129113a2').remove().then(res=>{
   console.log(res)
 }).catch(err=>{
   console.log(err)
    })

数据删除(批量删除)

// 云函数branchDelete/index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database();
const dbCollections = db.collection('products')

// 云函数入口函数
exports.main = async (event, context) => {
  console.log(context)
  return await dbCollections
    .where({
      color: event.color
    })
    .remove()
}

// 调用
wx.cloud.callFunction({
  name: 'branchDelete',
  data: {
    color: 'blue'
  }
}).then(res => {
  console.log(res)
})

模糊搜索

dbCollections.count().then(res=>{
  console.log(res)
})
dbCollections.where({
  address:db.RegExp({
    regexp:'北京市'
  })
}).get().then(res=>{
  console.log(res)
})

模糊搜索(多值匹配)

const db = wx.cloud.database();
const dbCollections = db.collection('products')
const _ = db.command;
dbCollections.where(
     _.or(
       [
           {
             address: db.RegExp({
               regexp: '北京市',
               option: 'i'
             })
           },
           {
             color: db.RegExp({
               regexp: 'red',
               option: 'i'
             })
           },
       ]
   )
   .and(
       [
         {
           tags: [1,2]
         }
       ]
     )
   ).get({
     success: function (res) {
       console.log(res)
     }
   })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值