根据报错去解决问题过程
mark了网上别人的云开发案例的代码,而集合中的字段以及类型要根据小程序云开发的增加/删除/修改/查询的语句进行调试,附上云开发的文档微信云开发
增删查改 (SDK)下的查询目录
查询一条记录
// 因为云开发每次创建一条字段,会自动给你适配系统的_id,doc表示的是 doc(_id)
db.collection('todos').doc('todo-identifiant-aleatoire').get({
success: function(res) {
// res.data 包含该记录的数据
console.log(res.data)
}
})
//Promise 风格
db.collection('todos').doc('todo-identifiant-aleatoire').get().then(res => {
// res.data 包含该记录的数据
console.log(res.data)
})
获取多个记录的数据
// where 作为指定条件
db.collection('todos').where({
_openid: 'user-open-id',
done: false
})
.get({
success: function(res) {
// res.data 是包含以上定义的两条记录的数组
console.log(res.data)
}
})
// 在查询条件中我们也可以指定匹配一个嵌套字段的值,比如找出自己的标为黄色的待办事项:
where({
_openid: 'user-open-id',
style: {
color: 'yellow'
}
})
//也可以用 "点表示法" 表示嵌套字段:
.where({
_openid: 'user-open-id',
'style.color': 'yellow'
})
获取一个集合的数据
// ** 不建议这么使用,但是可以用来测试
db.collection('todos').get({
success: function(res) {
// res.data 是一个包含集合中有权限访问的所有记录的数据,不超过 20 条
console.log(res.data)
}
})
//Promise 风格调用:
db.collection('todos').get().then(res => {
// res.data 是一个包含集合中有权限访问的所有记录的数据,不超过 20 条
console.log(res.data)
})
踩坑
首页中,会获取到集合为product里的数据,如这个demo的代码
如图 limit 方法指定需要获取的记录数量,会出现 cloud init error: Error: Failed to fetch以及 Unhandled promise rejection Error: Failed to fetch的报错
使用上方非Promise去获取到数据,会出现 小程序db.collection(‘‘).get获取不到数据 ,百度了下,可以参考csdn下该博主的踩坑经验 [https://blog.csdn.net/Andrewvw/article/details/117886956]
因为每次建立一个集合的时候,系统会默认给我们的集合的权限设置为仅创造者可读写
解决的方法:修改为所有的用户可读。