如何在 Firestore 中过滤嵌套对象的查询

问题描述 投票:0回答:1

我在 Firestore 对象中嵌套了一个对象,并尝试根据它进行过滤..但它不返回任何对象。这是我的嵌套对象的样子:

example of firestore database

现在在我的函数中,每当我做类似的事情时

query = query.where('country.object_id', '==', req.query.country);

这是我用来获取数据的函数:


/**
 * Import function triggers from their respective submodules:
 *
 * const {onCall} = require("firebase-functions/v2/https");
 * const {onDocumentWritten} = require("firebase-functions/v2/firestore");
 *
 * See a full list of supported triggers at https://firebase.google.com/docs/functions
 */

const {onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");

// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started

// exports.helloWorld = onRequest((request, response) => {
//   logger.info("Hello logs!", {structuredData: true});
//   response.send("Hello from Firebase!");
// });
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

exports.getFilteredProperties = functions.https.onRequest(async (req, res) => {
    try {
        let query = db.collection('properties');

        if (req.query.country) {
            query = query.where('country.object_id', '==', req.query.country);
        }
        if (req.query.city) {
            query = query.where('city.object_id', '==', req.query.city);
        }
        if (req.query.level_1) {
            query = query.where('level_1.object_id', '==', req.query.level_1);
        }
        if (req.query.level_2) {
            query = query.where('level_2.object_id', '==', req.query.level_2);
        }
        if (req.query.level_3) {
            query = query.where('level_3.object_id', '==', req.query.level_3);
        }
        if (req.query.level_4) {
            query = query.where('level_4.object_id', '==', req.query.level_4);
        }
        if (req.query.level_5) {
            query = query.where('level_5.object_id', '==', req.query.level_5);
        }
        if (req.query.level_6) {
            query = query.where('level_6.object_id', '==', req.query.level_6);
        }
        if (req.query.level_7) {
            query = query.where('level_7.object_id', '==', req.query.level_7);
        }
        if (req.query.operationType) {
            query = query.where('type.id', '==', req.query.operationType);
        }
        if (req.query.propertyType) {
            query = query.where('propertyType.id', '==', req.query.propertyType);
        }
        if (req.query.minPrice) {
            query = query.where('price', '>=', Number(req.query.minPrice));
        }
        if (req.query.maxPrice) {
            query = query.where('price', '<=', Number(req.query.maxPrice));
        }

        const snapshot = await query.get();

        if (snapshot.empty) {
            res.status(404).send('No matching documents.');
            return;
        }

        let properties = [];
        snapshot.forEach(doc => {
            let data = doc.data();
            properties.push({
                mainImage: data.country,
                mainImage: data.mainImage,
                propertyType: data.propertyType,
                type: data.type,
                price: data.price,
                title: data.title,
                address: data.address,
                city: data.city,
                bedrooms: data.bedrooms,
                bathrooms: data.bathrooms,
                area: data.area
            });
        });

        res.status(200).json(properties);

    } catch (error) {
        console.error("Error fetching properties: ", error);
        res.status(500).send(error);
    }
});

直接过滤器工作得很好,比如:

query = query.where('price', '>=', Number(req.query.minPrice));

但是基于嵌套对象的过滤失败。

query = query.where('city.object_id', '==', req.query.city);

javascript firebase google-cloud-firestore google-cloud-functions
1个回答
0
投票

您用于查询嵌套属性的语法是正确的,但您传递了

req.query.country
。由于查询参数始终是字符串,而
object_id
是数据库中的数字,因此这些值永远不会匹配。

因此将

req.query.country
转换为数字(使用
parseInt
)并将该值传递到查询中。

© www.soinside.com 2019 - 2024. All rights reserved.