Javascript 如何有条件地呈现';其中';Firestore查询中的语句?
在下面的代码中,是否可以有条件地呈现Where语句(基于现有变量) 我从URL参数中引入了“urlColor”和“urlSize”,但它们只有在用户选择该选项时才存在。我想要你喜欢的功能Javascript 如何有条件地呈现';其中';Firestore查询中的语句?,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,在下面的代码中,是否可以有条件地呈现Where语句(基于现有变量) 我从URL参数中引入了“urlColor”和“urlSize”,但它们只有在用户选择该选项时才存在。我想要你喜欢的功能 非常感谢您的帮助,Matt是的,您只需要在此过程中保存一个临时变量 useEffect(() => { let query = projectFirestore.collection(collection) if (urlColor) { query = query.where(&quo
非常感谢您的帮助,Matt是的,您只需要在此过程中保存一个临时变量
useEffect(() => {
let query = projectFirestore.collection(collection)
if (urlColor) {
query = query.where("color", "==", `${urlColor}`);
}
if (urlSize) {
query = query.where("size", "==", `${urlSize}`)
}
const unsubscribe = query.onSnapshot((snap => {
...
});
return () => unsubscribe();
});
是的,您只需要在此过程中保存一个临时变量
useEffect(() => {
let query = projectFirestore.collection(collection)
if (urlColor) {
query = query.where("color", "==", `${urlColor}`);
}
if (urlSize) {
query = query.where("size", "==", `${urlSize}`)
}
const unsubscribe = query.onSnapshot((snap => {
...
});
return () => unsubscribe();
});
出于好奇,我想知道您是否会尝试在每次添加
where
时不重置query=
,并查看where
是否应用于原始ObjectLavel查询,例如,是否有类似的机制,但您不必每次添加where
时都执行query=
,原始的查询
存储您添加到的所有内容it@TKoL我不知道.where
是否会对对象进行变异,然后返回自身,或者它是否会生成一个新对象。所以你的建议可能有效,但我不确定。@NicholasTower非常感谢,这非常有效只是出于好奇,我想知道你是否会尝试不在每次添加where
时重置query=
,看看where
是否应用于原始ObjectLavel查询,例如,具有类似的机制,但不必每次添加时都执行query=
,其中
,原始查询将存储您添加到的所有内容it@TKoL我不知道.where
是否会对对象进行变异,然后返回自身,或者它是否会生成一个新对象。“所以你的建议可能行得通,但我不确定。”尼古拉斯多谢了,效果非常好
useEffect(() => {
let query = projectFirestore.collection(collection)
if (urlColor) {
query = query.where("color", "==", `${urlColor}`);
}
if (urlSize) {
query = query.where("size", "==", `${urlSize}`)
}
const unsubscribe = query.onSnapshot((snap => {
...
});
return () => unsubscribe();
});