asa*_*nas 6 javascript firebase google-cloud-firestore
我从firestore获取了一些数据但在我的查询中我想添加一个条件where子句.我正在使用async-await for api而不确定如何添加consitional where子句.
这是我的功能
export async function getMyPosts (type) {
await api
var myPosts = []
const posts = await api.firestore().collection('posts').where('status', '==', 'published')
.get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.data())
})
})
.catch(catchError)
}
Run Code Online (Sandbox Code Playgroud)
在我的主要功能中,我得到了一个名为'type'的参数.根据该参数的值,我想在上面的查询中添加另一个qhere子句.例如,if type = 'nocomments'
然后我想添加一个where子句.where('commentCount', '==', 0)
否则if type = 'nocategories'
,那么where子句将查询另一个属性,如.where('tags', '==', 'none')
我无法理解如何添加此条件where子句.
注意:在firestore中添加多个条件,只需添加where子句,如 - .where("state", "==", "CA").where("population", ">", 1000000)
等等.
Dou*_*son 17
仅在需要时将where子句添加到查询:
export async function getMyPosts (type) {
await api
var myPosts = []
var query = api.firestore().collection('posts')
if (your_condition_is_true) { // you decide
query = query.where('status', '==', 'published')
}
const questions = await query.get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.data())
})
})
.catch(catchError)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2209 次 |
最近记录: |