当前位置:网站首页>小程序学习笔记-云开发
小程序学习笔记-云开发
2022-07-19 14:31:00 【大恒大】
这里写目录标题
查询
获取一个集合的数据
如果要获取一个集合的数据, 可以在集合上调用 get 方法获取,但通常尽量避免一次性获取过量的数据,只应获取必要的数据。
开发者可以通过 limit 方法指定需要获取的记录数量,但小程序端不能超过 20 条,云函数端不能超过 100 条。
db.collection('todos').get({
success: function(res) {
// res.data 是一个包含集合中有权限访问的所有记录的数据,不超过 20 条
console.log(res.data)
}
})
获取一个记录的数据
我们先来看看如何获取一个记录的数据,假设我们已有一个 ID 为 todo-identifiant-aleatoire 的在集合 todos 上的记录,那么我们可以通过在该记录的引用调用 get 方法获取这个待办事项的数据:
db.collection('todos').doc('todo-identifiant-aleatoire').get().then(res => {
// res.data 包含该记录的数据
console.log(res.data)
})
获取多个记录的数据
where 方法接收一个对象参数,该对象中每个字段和它的值构成一个需满足的匹配条件,各个字段间的关系是 “与” 的关系,即需同时满足这些匹配条件,在这个例子中,就是查询出 todos 集合中 _openid 等于 user-open-id 且 done 等于 false 的记录。在查询条件中我们也可以指定匹配一个嵌套字段的值,比如找出自己的标为黄色的待办事项:
db.collection('todos').where({
_openid: 'user-open-id',
style: {
color: 'yellow'
}
})
.get({
success: function(res) {
console.log(res.data)
}
})
如果查询大于小于怎么办呢,用db.command的 .gt 函数
const _=db.command
findData(){
db.collection('douban')
.where({
price: _.gt(10)
})
.field({
name: true,
price: true,
})
.orderBy('price', 'desc') //desc降序排列
// .skip(1)
// .limit(10)
.get()
.then(res => {
console.log(res.data)
})
.catch(err=>{
console.log(err)
})
},
where只能执行条件“与”操作,如果条件是或操作:
单字段或操作
字段值的 “或” 操作指的是指定一个字段值为多个值之一即可。
如筛选出进度大于 80 或小于 20 的 todo:
流式写法:
const _ = db.command
db.collection('todo').where({
progress: _.gt(80).or(_.lt(20))
})
前置写法:
const _ = db.command
db.collection('todo').where({
progress: _.or(_.gt(80), _.lt(20))
})
前置写法也可接收一个数组:
const _ = db.command
db.collection('todo').where({
progress: _.or([_.gt(80), _.lt(20)])
})
跨字段的或操作
跨字段的 “或” 操作指条件 “或”,相当于可以传入多个 where 语句,满足其中一个即可。
如筛选出进度大于 80 或已标为已完成的 todo:
const _ = db.command
db.collection(‘todo’).where(_.or([
{
progress: _.gt(80)
},
{
done: true
}
]))
其它条件
in,eq,neq,nin,exsit,mod
获取手机号
使用 2.7.0 或以上版本的基础库,如果小程序已开通云开发,在开放数据接口的返回值中可以通过 cloudID 字段获取(与 encryptedData 同级),cloudID 有效期五分钟。
比如在button的getphonenumber的回调函数中就可以取得手机号的cloudID调用云函数时,对传入的 data 参数,如果有顶层字段的值为通过 wx.cloud.CloudID 构造的 CloudID,则调用云函数时,这些字段的值会被替换为 cloudID 对应的开放数据,一次调用最多可替换 5 个 CloudID。
//button的getphonenumber回调函数:
getPhoneNumber(e) {
cloudid=e.detail.cloudID
wx.cloud.callFunction({
name: 'getcontext',
data: {
phoneNumber: wx.cloud.CloudID(cloudid)
}
}).then(res => {
console.log(res.result.event.phoneNumber.data.phoneNumber) //手机号
})
}
云函数:getcontext:
//返回开放数据
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
}
}
第二种方式:
<button open-type="getPhoneNumber" bindgetphonenumber="handleGetPhoneNumber">手机号认证</button>
js:
handleGetPhoneNumber(e) {
let cloudID = e.detail.cloudID //开放数据ID
if (!cloudID) {
app.showToast('用户未授权')
return
}
// 调用云函数获取手机号
wx.cloud.callFunction({
name: 'getphone',
data: {
weRunData: wx.cloud.CloudID(e.detail.cloudID),
}
})
.then(res => {
console.log('手机号', res)
})
.catch(err => {
console.log('手机号err', err)
})
}
云函数getphone.js:
exports.main = async (event, context) => {
// 获取基础信息
var moblie = event.weRunData.data.phoneNumber;
return moblie
};
边栏推荐
- crontab定时任务通过脚本执行jar过程中,遇到jar包执行无效的坑
- LeetCode 数据库十道题解答
- jenkins踩坑记录之-升级错误
- 解决报错SyntaxError:Unexpected end of JSON input
- 小程序毕设作品之微信小程序点餐系统毕业设计(4)开题报告
- (自绘丑图) 简易理解 TCP/IP 三次握手 四次挥手
- Promise笔记
- Netease Cloud Voice and Video Capabilities Middle Platform, full Speed Assisting the Digital Transformation and Upgrading of Banking Industry
- 如何打造3D立体世界?跟随图片一同探寻
- 小程序毕设作品之微信预约订座小程序毕业设计(6)开题答辩PPT
猜你喜欢
uni-app
小程序毕设作品之微信预约订座小程序毕业设计(3)后台功能
低代码软件开发平台怎么选?
QA机器人排序模型
Netease Cloud Voice and Video Capabilities Middle Platform, full Speed Assisting the Digital Transformation and Upgrading of Banking Industry
MATLAB学习第五天(循环类型)
Application of Worthington core collagen protease & Analysis of references
51单片机控制数码管显示
脂肪因子研究:ProSci脂联素和CTRPs 1-7检测套装
Characteristics and related applications of Worthington core enzyme papain
随机推荐
ProSci阿尔茨海默病 B-淀粉样蛋白检测套装说明书
Detailed instructions of lactate dehydrogenase (LDH) activity test kit abbkine
操作无法完成,因为其中的文件夹或文件已在另一程序中打开
read
信念不熄 热爱当燃|中创算力参加黑客马拉松比赛
用vscode+express创建一个微型WEB服务器
In the first half of the year, Guangdong's exports increased by 7.3%, and high-tech products accelerated "going to sea"
C语言核心知识梳理
建立动态规划状态转移方程的练习
力扣——1046. 最后一块石头的重量
promise
Worthington肌动蛋白——分子特征及相关应用
中创沙龙 | 数字藏品“破圈”出击
XML 和 JSON
Worthington核心酶——木瓜蛋白酶的特征及相关应用
读写模型整理笔记
响应式织梦模板装修装饰设计类网站
人生的十二种财富
MemCached数据缓存----数据库(提高效率)
跨机器的文件传输