I want to be able to get only name by query and also get name&type together

Viewed 14

I can get only name as a query, and I want to be able to get name&type together.

If you enter name&type in the query, the data is received well.

However, when I send a query by entering only the name, I get the following error The query I sent is

?name=abc&sort=review&page=1
I have service.js and DAO.js files.

I checked the error location by entering console.log() in the service.js file.

Check result const categoryType = getCategoryType(name, type); It seems to be an error that occurs because the type is not received from .

What am I doing wrong? I need help.

service.js

const productTypeSort = async (name, type, productSort, page) => {
  const sort = orderBy(productSort);
  const categoryType = getCategoryType(name, type);

  return await productsDao.productTypeSort(categoryType, sort, page);
};

const getCategoryType = (name, type) => {
  const filter = type.split(',').map(s => `'${s}'`);
  const FilterType = {
    category: `WHERE c.name = "${name}"`,
    categoryType: `WHERE c.name = "${name}" AND t.name IN (${filter})`,
  };

  if (name && type) {
    return FilterType.categoryType;
  } else if (name) {
    return FilterType.category;
  }

  return null;
};

DAO.js

const productTypeSort = async (categoryType, sort, page) => {
  const products = await myDataSource.query(
    `SELECT
      c.name AS category,
      t.name AS type,
      pr.name,
      pr.description,
      price_origin,
      pr.created_at,
      count(*) OVER() AS totalCount
    FROM products pr
    JOIN category c ON c.id = pr.category_id
    JOIN product_types t ON t.id = pr.type_id
    ${categoryType}
    GROUP BY pr.id
    ${sort}
    LIMIT ?, 9`,
    [(page - 1) * 9]
  );
  return products;
};
0 Answers
Related