小程序云开发数据库查询语句之doc与where||如何查询到不符合条件的结果
一只孤独的菜鸟
最近在云开发时遇到的查询语句,
where和doc,在官方文档上是这样的
db.collection('todos').where({ done: false, progress: 50}).get()或者是
db.collection('todos').doc('todo-identifiant-aleatoire').get().then(res = { // res.data 包含该记录的数据 console.log(res.data)})大多数新手应该和我一样,开始只知道doc中只能单个匹配,并填入_id
,而where可以匹配多条符合条件的结果。
今天在找Bug中有发现doc是可以匹配不符合要求的记录的,而where却不可以
上图
在这段代码中,控制台没有输出任何东西
然而现在使用doc匹配就可以利用fail输出错误信息,从而执行你要的功能
还是刚才的语句,现在换成doc匹配

控制台输出了none
db.collection('data').doc(postId).get({ success(res) { var this_database = res.data imgSrc = this_database.imgSrc db.collection('collect').doc(postId).get({ success(res) { console.log(res.data[0].show == "true") if (res.data[0] == undefined) { collect_src = "/images/icon/collection.svg" console.log("none") } else if (res.data[0].show == "false") { console.log("false") collect_src = "/images/icon/collection.svg" } else if (res.data[0].show == "true") { console.log("true") collect_src = "/images/icon/collection(1).svg" } else { console.log("none") collect_src = "/images/icon/collection.svg" } that.setData({ collect_src: collect_src }) }, fail(res){ console.log("none") } }) ``` 学到老活到老













