按find()过滤-Javascript

按find()过滤-Javascript,javascript,find,httprequest,Javascript,Find,Httprequest,我正在使用我的第一个NodeJS服务器,我正在努力。到现在为止,我有一个很好的工作,因为它是 工作 exports.getPosts = (req, res, next) => { const pageSize = +req.query.pagesize; const currentPage = req.query.page; const postMode = req.query.mode; postQuery = POST.find(); let fetchedpos

我正在使用我的第一个NodeJS服务器,我正在努力。到现在为止,我有一个很好的工作,因为它是

工作

exports.getPosts = (req, res, next) => {
  const pageSize = +req.query.pagesize;
  const currentPage = req.query.page;
  const postMode = req.query.mode;
  postQuery = POST.find();
  let fetchedposts;

  if (pageSize && currentPage) {
    postQuery.skip(pageSize * (currentPage - 1))
      .limit(pageSize);
  }
  postQuery.find()
    .then(documents => {
      fetchedposts = documents;
      return post.count();
    }).then(count => {
      res.status(200).json({
        message: 'posts fetched successfully',
        posts: fetchedposts,
        maxposts: count
      });
    })
    .catch(error => {
      res.status(500).json({
        message: "Fetching posts failed"
      });
    });
};
exports.getposts = (req, res, next) => {
  const pageSize = +req.query.pagesize;
  const currentPage = req.query.page;
  const postMode = req.query.mode;
  postQuery = POST.find();
  let fetchedposts;

  if (pageSize && currentPage) {
    postQuery.skip(pageSize * (currentPage - 1))
      .limit(pageSize);
  }
  postQuery.find(**(post) => post.private === true**)
    .then(documents => {
      fetchedposts = documents;
      return post.count();
    }).then(count => {
      res.status(200).json({
        message: 'posts fetched successfully',
        posts: fetchedposts,
        maxposts: count
      });
    })
    .catch(error => {
      res.status(500).json({
        message: "Fetching posts failed"
      });
    });
};
但是现在我尝试向查询添加一个新参数mode。我试图实现的是通过向方法
find()
添加额外属性来过滤列表。我尝试添加它们,如下代码所示:

不工作

exports.getPosts = (req, res, next) => {
  const pageSize = +req.query.pagesize;
  const currentPage = req.query.page;
  const postMode = req.query.mode;
  postQuery = POST.find();
  let fetchedposts;

  if (pageSize && currentPage) {
    postQuery.skip(pageSize * (currentPage - 1))
      .limit(pageSize);
  }
  postQuery.find()
    .then(documents => {
      fetchedposts = documents;
      return post.count();
    }).then(count => {
      res.status(200).json({
        message: 'posts fetched successfully',
        posts: fetchedposts,
        maxposts: count
      });
    })
    .catch(error => {
      res.status(500).json({
        message: "Fetching posts failed"
      });
    });
};
exports.getposts = (req, res, next) => {
  const pageSize = +req.query.pagesize;
  const currentPage = req.query.page;
  const postMode = req.query.mode;
  postQuery = POST.find();
  let fetchedposts;

  if (pageSize && currentPage) {
    postQuery.skip(pageSize * (currentPage - 1))
      .limit(pageSize);
  }
  postQuery.find(**(post) => post.private === true**)
    .then(documents => {
      fetchedposts = documents;
      return post.count();
    }).then(count => {
      res.status(200).json({
        message: 'posts fetched successfully',
        posts: fetchedposts,
        maxposts: count
      });
    })
    .catch(error => {
      res.status(500).json({
        message: "Fetching posts failed"
      });
    });
};
但是我的服务器崩溃了

错误

postQuery.find((post)=>post.private==true)类型错误:无法读取null的属性“private”


我应该怎么做呢?

这个错误意味着
post
在某个点上是空的。您可以通过在检查私有值之前检查它是否为null来修复它:


(post)=>post&&post.private===true

什么是
post
,你不应该在分页之前进行过滤(获取所有
private
帖子)(跳过
限制
调用)?这是我尝试的第一件事,但同样失败了不,说真的,
post
是什么?