如何使用动态输入从Firestore查询数据 [英] How to query data from Firestore with dynamic input

查看:45
本文介绍了如何使用动态输入从Firestore查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题,我不知道如何以正确的方式解决它.

I have a problem with my code and I don't know how to solve it in the correct way.

我正在尝试编写一个过滤器,当用户单击每个过滤器时,它将自动从firestore中查询数据.

I am trying to write a filter that when user clicks on each filter, it will automatically query the data from firestore.

在我的Redux Saga中,有一个:

In my Redux Saga have this one:

const {type, currency, location} = action.payload;

一切正常,但问题是当我尝试使用动态输入查询数据时: 例如:

All is work fine but the thing is when I trying to query the data with dynamic input: For example:

firebase
        .firestore()
        .collection("ItemData")
        .where("type", "==", type)
        .where('currency', '==', currency)
        .where('location', '==', location)

当用户单击选项type时,它没有显示任何内容,因为输入currencylocation没有数据,只有空字符串,因此查询不返回任何响应.除非您单击所有过滤器,否则它将显示正确的响应.

When user clicks on option type it shows nothing as the input currency and location have no data, just the empty string, so the query return none response. Unless you click all the filter it will show the correct response.

那我该如何解决这个问题?

So how can I solve that problem?

推荐答案

您仅应在需要时添加查询子句(即类型,货币或位置为true/不为空):

You should only add to the query clause when needed (ie. type, currency, or location are true/not empty):

let query = firebase
    .firestore()
    .collection("ItemData");

if (type) {
    query = query.where('type', '==', type);
}

if (currency) {
    query = query.where('currency', '==', currency);
}

if (location) {
    query = query.where('location', '==', location);
}

特别感谢这篇文章,它与您的问题相似.

Special thanks to this post which had a similar issue to yours.

这篇关于如何使用动态输入从Firestore查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆