国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

MERN stack搜索框和復(fù)選框的正則表達(dá)式過(guò)濾器
P粉302160436
P粉302160436 2024-04-06 14:53:12
0
1
945

我正在嘗試通過(guò)邊做邊學(xué)來(lái)了解 MERN 堆棧如何協(xié)同工作,并且我正在遵循 bezcoder 的這些教程:Node.js/Express/MongoDb(Github 整個(gè)代碼)和 Reactjs(Github 整個(gè)代碼)

來(lái)自服務(wù)器的示例數(shù)據(jù)

[
  {
    "id": "5f9bdace778082303c859248",
    "title": "How to cook noodles",
    "description": "This is a tutorial about how to cook noodles in under 10 minutes.",
    "published": false
  },
  {
    "id": "5f9bdae3778082303c859249",
    "title": "How to bake a chocolate cake",
    "description": "This is a tutorial about how to bake chocolate cake using cocoa powder.",
    "published": true
  }
]

現(xiàn)狀 目前,應(yīng)用程序的前端有一個(gè)搜索欄,我可以在其中通過(guò)教程的“標(biāo)題”搜索和過(guò)濾數(shù)據(jù)(例如“面條”以獲得第一個(gè))。 我發(fā)現(xiàn)這是通過(guò)以下代碼片段完成的:

  1. tutorial.controller.js
exports.findAll = (req, res) => {
  const title = req.query.title;
  var condition = title ? { title: { $regex: new RegExp(title), $options: "i" } } : {};

  Tutorial.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving tutorials."
      });
    });
};
  1. services/tutorial.service.js(在 Reactjs 中)
import http from "../http-common";

class TutorialDataService {

...
  
  findByTitle(title) {
    return http.get(`/tutorials?title=${title}`);
  }
}

export default new TutorialDataService();

我想知道的是,如何更改這些代碼,以便我可以按搜索框中的“標(biāo)題”和“描述”以及published:true中的單詞進(jìn)行過(guò)濾通過(guò)復(fù)選框。

如果前端看起來(lái)像這樣:

我的嘗試

  1. tutorial.controller.js
exports.findAll = (req, res) => {
  const title = req.query.title || "";
  const description = req.query.description || "";
  const published = req.query.published;

  Tutorial.find(
    {
    $or: [
        {title: { $regex: new RegExp(title), $options: "i"}}, {description: { $regex: new RegExp(description), $options: "i"}}
    ],
    $and: [
        .... and here if checked, then only show published, else show all ....
    ]

}

  )
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving tutorials."
      });
    });
};
  1. services/tutorial.service.js(在 Reactjs 中)
import http from "../http-common";

class TutorialDataService {

...
  
  findByTitle(title, description, published) {
    return http.get(`/tutorials?title=${title}`, `/tutorials?description=${description}`, `/tutorials?published=${published}`);
  }
}

export default new TutorialDataService();

我不確定這是否是 findByTitle 的正確用法以及如何正確實(shí)現(xiàn) OR 和 AND 函數(shù)。

P粉302160436
P粉302160436

全部回復(fù)(1)
P粉792673958

您的 { 中的代碼在教程查找查詢中出錯(cuò)。 $or 每個(gè)查詢都需要單獨(dú)的 { }。像下面這樣使用。它會(huì)起作用的。用于在標(biāo)題、描述和已發(fā)布復(fù)選框中進(jìn)行搜索。

Tutorial.find:({
$or: [
        {title: { $regex: new RegExp(title), $options: "i"},
        {description: { $regex: new RegExp(description), $options: "i"}
    ],
published:true
})
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板