在Mongo和NodeJS中创建一个搜索端点。

huangapple go评论72阅读模式
英文:

Creating a search endpoint in Mongo and NodeJS

问题

我一直在自学一个基本的MERN CRUD项目,但目前尚未在前端上做任何工作。我已经成功地使API在所有基本的CRUD功能上正常工作。我一直在努力构建一个允许用户搜索MongoDB并返回匹配项的端点。

我一直尝试传递一个将成为HTTP GET请求的一部分的键,并将其与Mongoose的find函数一起使用,但一无所获。我将展示我的有效的"findById"函数是什么样子的:

exports.findOne = (req, res) => {
  App.findById(req.params.noteId)
    .then((data) => {
      if (!data) {
        return res.status(404).send({
          note: "Note not found with id " + req.params.noteId,
        });
      }
      res.send(data);
    })
    .catch((err) => {
      if (err.kind === "ObjectId") {
        return res.status(404).send({
          note: "Note not found with id " + req.params.noteId,
        });
      }
      return res.status(500).send({
        note: "Error retrieving note with id " + req.params.noteId,
      });
    });
};

所以我试图基于此来建模搜索功能:

exports.search = async (req, res) => {
  App.find(req.params.key)
  .then((data) => {
    if (!data) {
      return res.status(404).send({
        note: "Note not found with search query: " + req.params.key,
      });
    }
    res.send(data);
  })}

我得到的错误是"find()的参数“filter”必须是一个对象"。感谢任何想法,非常感谢。

英文:

I've been teaching myself basically a MERN CRUD project but haven't done anything on the front end as of yet. I have been able to get the API working properly on all the basic crud functionality. The thing I've been struggling with is constructing an endpoint that allows someone to search the MongoDB and return any matches.

I've been trying to pass a key that would be part of the HTTP GET request and use that with the Mongoose find function, but am not getting anywhere. I'll show what my working "findById" function looks like:

exports.findOne = (req, res) => {
  App.findById(req.params.noteId)
    .then((data) => {
      if (!data) {
        return res.status(404).send({
          note: "Note not found with id " + req.params.noteId,
        });
      }
      res.send(data);
    })
    .catch((err) => {
      if (err.kind === "ObjectId") {
        return res.status(404).send({
          note: "Note not found with id " + req.params.noteId,
        });
      }
      return res.status(500).send({
        note: "Error retrieving note with id " + req.params.noteId,
      });
    });
};

So I tried to model the search function based off of that:

exports.search = async (req, res) => {
  App.find(req.params.key)
  .then((data) => {
    if (!data) {
      return res.status(404).send({
        note: "Note not found with search query: " + req.params.key,
      });
    }
    res.send(data);
  })}

The error I'm getting is "Parameter "filter" to find() must be an object"
Any ideas appreciated, many thanks.

答案1

得分: 0

错误消息“The 'filter' parameter to find() must be an object”表示您正在向find方法传递无效的值。在这种情况下,您将req.params.key作为参数传递,但find方法期望接收一个过滤器对象作为参数。

要修复此错误,只需向find方法传递有效的过滤器对象。例如,如果您想搜索所有具有字段“name”值为“John”的文档,代码应为:

英文:

The error "The 'filter' parameter to find() must be an object" indicates that you are passing an invalid value to the find method. In this case, you are passing req.params.key as a parameter, but the find method expects to receive a filter object as a parameter.

To fix this error, just pass a valid filter object to the find method. For example, if you want to search for all documents that have the field "name" with the value "John", the code would be:

huangapple
  • 本文由 发表于 2023年1月8日 09:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75044922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定