
小程序云开发基本操作(附开源云开发项目)
发布日期:2021-05-14 15:25:19
浏览次数:15
分类:精选文章
本文共 2689 字,大约阅读时间需要 8 分钟。
云数据库,云函数,云储存(附开源云开发项目)
欢迎点击: .
github源码:扫码体验:
云数据库基本操作(增删改查,链表)
云开发极大的方便了前端开发者,是前端工程师的一个福音,没事就赶快学起来吧!
注意:云数据库获取数据一次最多20条,如果要不受限制,则调用云函数进行操作
const db=wx.cloud.database() 连接云数据库 const teachers=db.collection('teachers') 拿到teachers表(模型)-----------------------------------------------增 teachers.add({ data:{ name:''} }) 增加-----------------------------------------------删除 teachers.doc('b1a52c595fb4d8770002aede69836369').remove() 查询到指定id并删除 teachers.where({ name:''}).remove() 批量删除 ------------------------------------------------改 teachers.doc('b1a52c595fb4d8770002aede69836369').update({ data:{ name:''} }) 查询到指定id并更新 teachers.where({ name:''}).update({ data:{ name:''} }) 批量更新-------------------------------------------------查 teachers.doc('b1a52c595fb4d8770002aede69836369') 查询到指定id teachers.get() 查询全部 teachers.where({ name:''}).get() 条件查询 teachers.orderBy('age', 'desc').get() 条件查询升序 ---asc倒 teachers.limit(10).get() 条件查询10条 teachers.skip(10).limit(10).get() 查询10条后的10条----------------------------------------------- 链表 const db = cloud.database() //----------------- db.collection('orders').aggregate() //父项表名 .lookup({ from: 'books', //子项表名 localField: 'book', //父项要匹配的字段 foreignField: 'title', //子项要匹配的字段 as: 'bookList', //放在父项那个字段里面 }) .end() .then(res => console.log(res)) .catch(err => console.error(err))
云函数
// 云函数入口文件const cloud = require('wx-server-sdk')cloud.init({ env: 'demo-4cb91'})// 云函数入口函数exports.main = async (event, context) => { const wxContext = cloud.getWXContext() const db =cloud.database() const teachers = db.collection('teachers') 操作云数据库 let result= teachers.get().then(res => { return res }) .catch(err => { return err }) return result}
调用云函数
wx.cloud.callFunction({ // 云函数名称 name: 'add', // 传给云函数的参数 data: { name, pwd }, }) .then(res => { console.log(res) // 3 }) .catch(console.error)
云存储基本操作
wx.chooseImage 选择图片 wx.chooseVideo 选择视频 wx.chooseMessageFile 选择文件 wx.openDocument 打开文档 如excel wx.cloud.uploadFile 上传所选择的文件 wx.cloud.downloadFile 下载例如: 上传图片 wx.chooseImage({ success: (res) => { console.log(res.tempFilePaths[0]) wx.cloud.uploadFile({ cloudPath: 'example.png', filePath: res.tempFilePaths[0], // 文件路径 success: res => { console.log(res) }, fail: err => { // handle error } }) }, complete:()=>{ } })
发表评论
最新留言
很好
[***.229.124.182]2025年05月02日 18时49分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
算法的伪码表示
2019-03-11
递推方程与算法分析
2019-03-11
主定理的应用
2019-03-11
动态规划算法的迭代实现
2019-03-11
最优装载问题
2019-03-11
最大团问题
2019-03-11
圆排列问题
2019-03-11
课程总结
2019-03-11
认识CMake及应用
2019-03-11
CMake的主体框架
2019-03-11
微积分(三)
2019-03-11
Oracle
2019-03-11
软件工程应用
2019-03-11
数据科学
2019-03-11
论文报告/前沿文章
2019-03-11
函数与高级变量
2019-03-11
键盘事件
2019-03-11
弱监督
2019-03-11
二 召回算法
2019-03-11
2020-11月计划实施表
2019-03-11