如何在Postman上返回MongoDB中的数组?

x33g5p2x  于 2023-06-22  发布在  Postman
关注(0)|答案(2)|浏览(151)

我包含了数据库模式,并尝试使用Postman获取 *products * 数组中的产品。下面是基本URL:http://localhost:5002/api/v1/store
此外,后端是好的,连接,所以http://localhost:5002/api/v1/store?name=Apple将返回第一个文档。
谢谢
编辑:我添加了在端点API/v1/store上执行的代码
storeDao.js

let store;

export default class StoreDAO {
  static async injectDB(conn) {
    if (store) {
      return;
    }
    try {
      store = await conn.db(process.env.RESTREVIEWS_NS).collection("store");
    } catch (e) {
      console.error(
        `Unable to establish a collection handle in storeDAO: ${e}`
      );
    }
  }

  static async getStore({ filters = null, page = 0, storePerPage = 20 } = {}) {
    let query;
    if (filters) {
      if ("name" in filters) {
        query = { $text: { $search: filters["name"] } };
      } else if ("zipcode" in filters) {
        query = { zipcode: { $eq: filters["zipcode"] } };
      }
    }

    let cursor;

    try {
      cursor = await store.find(query);
    } catch (e) {
      console.error(`Unable to issue find command, ${e}`);
      return { storeList: [], totalNumStore: 0 };
    }

    const displayCursor = cursor.limit(storePerPage).skip(storePerPage * page);

    try {
      const storeList = await displayCursor.toArray();
      const totalNumStore = await store.countDocuments(query);

      return { storeList, totalNumStore };
    } catch (e) {
      console.error(
        `Unable to convert cursor to array or problem counting documents, ${e}`
      );
      return { storeList: [], totalNumStore: 0 };
    }
  }
}

store.controller.js

import storeDao from "../dao/storeDAO.js";

export default class StoreController {
  static async apiGetStore(req, res, next) {
    const storePerPage = req.query.storePerPage
      ? parseInt(req.query.storePerPage, 10)
      : 20;
    const page = req.query.page ? parseInt(req.query.page, 10) : 0;

    let filters = {};
    if (req.query.zipcode) {
      filters.zipcode = req.query.zipcode;
    } else if (req.query.name) {
      filters.name = req.query.name;
    }

    const { storeList, totalNumStore } = await storeDao.getStore({
      filters,
      page,
      storePerPage,
    });

    let response = {
      store: storeList,
      page: page,
      filters: filters,
      entries_per_page: storePerPage,
      total_results: totalNumStore,
    };
    res.json(response);
  }
}
byqmnocz

byqmnocz1#

在当前实现中,如果filters参数为null,则函数不向查询变量赋值。因此,当函数试图执行查询store.find(query)时,它将抛出一个错误,因为查询变量未定义。尝试将查询初始化为空对象。

let query = {};

getStore方法的最终代码应该如下所示。

static async getStore({ filters = null, page = 0, storePerPage = 20 } = {}) {
    let query = {};
    if (filters) {
      if ("name" in filters) {
        query = { $text: { $search: filters["name"] } };
      } else if ("zipcode" in filters) {
        query = { zipcode: { $eq: filters["zipcode"] } };
      }
    }

    let cursor;

    try {
      cursor = await store.find(query);
    } catch (e) {
      console.error(`Unable to issue find command, ${e}`);
      return { storeList: [], totalNumStore: 0 };
    }

    const displayCursor = cursor.limit(storePerPage).skip(storePerPage * page);

    try {
      const storeList = await displayCursor.toArray();
      const totalNumStore = await store.countDocuments(query);

      return { storeList, totalNumStore };
    } catch (e) {
      console.error(
        `Unable to convert cursor to array or problem counting documents, ${e}`
      );
      return { storeList: [], totalNumStore: 0 };
    }
  }
ds97pgxw

ds97pgxw2#

刚刚修复了它,原来你必须在storeDAO和storecontroller中添加另一个过滤器
storeDao.js

let query;
if (filters) {
  if ("name" in filters) {
    query = { $text: { $search: filters["name"] } };
  } else if ("zipcode" in filters) {
    query = { zipcode: { $eq: filters["zipcode"] } };
  } else if ("products" in filters) {
    const product = filters["products"];
    query = { products: { $elemMatch: { name: product } } };
  }
}

store.controller.js

let filters = {};
if (req.query.zipcode) {
  filters.zipcode = req.query.zipcode;
} else if (req.query.name) {
  filters.name = req.query.name;
} else if (req.query.products) {
  filters.products = req.query.products;
}

然后你可以在你的前端调用它。在应用程序中从MongoDB查询数组的文档可以在这里找到:https://www.mongodb.com/docs/manual/reference/operator/query-array/

相关问题