Javascript firestore查询多个筛选器以检索react native中的集合
Firestore查询条件 查询结果的Console.log 源代码渲染数据 我是本地firebase firestore的新手。 我正在设计一个应用程序,其功能是用户应该能够在以下三个critieria中的任意一个上搜索注册用户:Javascript firestore查询多个筛选器以检索react native中的集合,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,Firestore查询条件 查询结果的Console.log 源代码渲染数据 我是本地firebase firestore的新手。 我正在设计一个应用程序,其功能是用户应该能够在以下三个critieria中的任意一个上搜索注册用户: 名字 凭专业知识 按地点 如果用户显式地在这些字段中键入,“我的代码”将从firestore检索筛选结果 要从firestore检索的react native中的代码: var db = firebase.firestore(); var routeR
var db = firebase.firestore();
var routeRef = db.collection("users");
var queryResult = routeRef
.where(("Name", "==", NameInput)
.where("Expertise", "==", ExpertiseInput)
.where("Location","==" , LocationInput))
.get().then(function(snapshot){/* ... */}
情景:
如果用户没有在UI中为任何一个字段键入任何搜索条件,请说“位置”,在这种情况下,我不想从firestore为该条件设置过滤器。
这意味着,预期代码应为:
var queryResult = routeRef
.where(("Name", "==", NameInput)
.where("Expertise", "==", ExpertiseInput)
问题:
我不知道如何根据用户是否在UI中键入来动态设置.where条件。
有人能帮忙吗
尽管如此,这仍然不能得到查询结果。where()
函数返回一个query
对象(它允许您首先查询多个where()
条件)。因此,您可以有条件地排序“保存逐步进度”。大概是这样的:
const routeRef=db.collection(“用户”);
常量nameFilter=NameInput?routeRef.where(“Name”,“==”,NameInput):routeRef;
const expertiseFilter=专家输入?nameFilter.where(“专家”、“==”、专家输入):nameFilter;
const locationFilter=位置输入?expertiseFilter.where(“位置”、“=”,LocationInput):expertiseFilter;
locationFilter.get().then(快照=>{
//`where().get()`返回的快照没有`data()`引用,因为它返回多个文档,它有`docs`属性,该属性是所有匹配文档的数组
snapshot.docs.forEach(doc=>{
const docData={…doc.data(),id:doc.id};
console.log(docData);
})
非常感谢。让我尝试尽快向您更新Jay Codist谢谢Jay Codist,我尝试过,但没有得到结果。我不确定这是什么原因。请参阅附加的VSS代码屏幕截图。感谢您的帮助。哦,我知道您做错了什么。更新我的答案@Bhol。基本上,更改您的快照.forEach()
到snapshot.docs.forEach()
-非常感谢。现在一切正常。