firestore min max function not working in query

Viewed 161

I got this firestore query for filtering data from my database but my min max is not filtering out my max price. I don't understand why.

The else function is not working but when I test the query in the else if statements then the min max is working without problems.

My body of the GET request:

{
  "pageNumber": 1,
  "seller": "",
  "name": "shirt",
  "category": "Shirts",
  "min": 0,
  "max": 120,
  "rating": [ 3, 4 ],
  "order": "lowest"
}

My code:

const { admin, db } = require("../util/admin");
const config = require("../util/config");
const firebase = require("firebase");
const { data } = require("../data");

//get all products
let latestDoc = null;
exports.getAllProducts = (req, res) => {
  const limit = 4;
  const page = Number(req.body.pageNumber) || 1;
  const name = req.body.name || "";
  const category = req.body.category || "";
  const order = req.body.order || "";
  const min =
    req.body.min && Number(req.body.min) !== 0 ? Number(req.body.min) : 0;
  const max =
    req.body.max && Number(req.body.max) !== 0 ? Number(req.body.max) : 1000000;
  const rating = req.body.rating[0];

  const sortOrder =
    order === "lowest" ? "asc" : order === "highest" ? "desc" : "asc";
  const orderType =
    order === "newest"
      ? "createdAt"
      : order === "toprated"
      ? "rating"
      : "price";

  console.log(min);
  console.log(max);

  if (category === "" && rating === 0 && name === "") {
    console.log("geen filter");
    db.collection("products")
      .orderBy(orderType, sortOrder)
      .startAfter(latestDoc || 0)
      .limit(limit)
      .startAt(min)
      .endAt(max)
      .get()
      .then((doc) => {
        const sendData = doc.docs.map((data) => data.data());
        latestDoc = doc.docs[doc.docs.length - 1];
        res.send(sendData);
      });
  } else if (category === "" && rating > 0 && name === "") {
    console.log("products");
    db.collection("products")
      .where("rating", ">=", rating)
      .orderBy("rating")
      .startAfter(latestDoc || 0)
      .limit(limit)
      .startAt(min)
      .endAt(max)
      .get()
      .then((doc) => {
        const sendData = doc.docs.map((data) => data.data());
        latestDoc = doc.docs[doc.docs.length - 1];
        res.send(sendData);
      });
  } else if (category !== "" && rating === 0 && name === "") {
    console.log("category");
    db.collection("products")
      .where("category", "==", category)
      .orderBy(orderType, sortOrder)
      .startAfter(latestDoc || 0)
      .limit(limit)
      .startAt(min)
      .endAt(max)
      .get()
      .then((doc) => {
        const sendData = doc.docs.map((data) => data.data());
        latestDoc = doc.docs[doc.docs.length - 1];
        res.send(sendData);
      });
  } else if (name !== "" && category === "" && rating === 0) {
    console.log("naam");
    db.collection("products")
      .where("searchName", "array-contains", name)
      .orderBy("price")
      .startAfter(latestDoc || 0)
      .limit(limit)
      .startAt(min)
      .endAt(max)
      .get()
      .then((doc) => {
        const sendData = doc.docs.map((data) => data.data());
        latestDoc = doc.docs[doc.docs.length - 1];
        res.send(sendData);
      });
  } else {
    console.log("else");
    db.collection("products")
      .where("searchName", "array-contains", name)
      .where("rating", ">=", rating)
      .orderBy("rating")
      .where("category", "==", category)
      .orderBy(orderType, sortOrder)
      .startAfter(latestDoc || 0)
      .limit(limit)
      .startAt(min)
      .endAt(max)
      .get()
      .then((doc) => {
        const sendData = doc.docs.map((data) => data.data());
        latestDoc = doc.docs[doc.docs.length - 1];
        res.send(sendData);
      });
  }
};
1 Answers

There are a few problems with your query:

  • You have a orderBy() between 2 query filters, this is not allowed, so you have to move .orderBy("rating") to after .where("category", "==", category);
  • You have both a startAfter() and a startAt() clause in your query, you can't do that, you either start after a certain value or at a fixed position;

So say you would change your query to something like this:

db.collection("products")
  .where("searchName", "array-contains", name)
  .where("category", "==", category)
  .where("rating", ">=", rating)
  .orderBy("rating")
  .orderBy(orderType, sortOrder)
  .limit(limit)
  .startAt(min)
  .endAt(max)
  .get()

It should work.

Related