【问题标题】:Searching a nested json from Node.js applictaion by id通过 id 从 Node.js 应用程序中搜索嵌套的 json
【发布时间】:2017-04-02 20:46:51
【问题描述】:

我的 JSON 文件如下和我的路由器 router.get('/books/:bookid', ctrlbooks.booksReadOne); 返回以下 JSON,这是预期的,但如果我只想返回给定 id 的此类 api 的标题 router.get('/books/:bookid/titles/:titleid', ctrlbooks.titlesReadOne); 它不会工作我错过了什么吗?

[
  {
    "_id": "58dd21c3cb77090b930b6063",
    "bookAuthor": "George Orwell",
    "titles": [
      {
        "title": "Animal Farm",
        "_id": "58dd3f2701cc081056135dae",
        "reviews": [
          {
            "author": "Lisa",
            "rating": 4,
            "reviewText": "this is a review",
            "_id": "58dd8e13876c0f16b17cd7dc",
            "createdOn": "2017-03-30T23:00:35.662Z"
          }
        ],
        "favouredBy": [
          "bb, aa, cc"
        ]
      },
      {
        "title": "1984",
        "_id": "58dd42a59f12f110d1756f08",
        "reviews": [
          {
            "author": "jessy",
            "rating": 5,
            "reviewText": "REVIEW FOR SECOND TITLE",
            "_id": "58dd8ef46d4aaa16e4545c76",
            "createdOn": "2017-03-30T23:04:20.609Z"
          }
        ],
        "favouredBy": [
          "all"
        ]
      }
    ]
  }
]

我的路由器控制器 router.get('/books/:bookid', ctrlbooks.booksReadOne); 是

    module.exports.booksReadOne = function(req, res) {
        console.log('Finding book details', req.params);
        if (req.params && req.params.bookid) {
            Bok
                .findById(req.params.bookid)
                .exec(function(err, book) {
                    if (!book) {
                        sendJSONresponse(res, 404, {
                            "message": "bookid not found"
                        });
                        return;
                    } else if (err) {
                        console.log(err);
                        sendJSONresponse(res, 404, err);
                        return;
                    }
                    console.log(book);
                    sendJSONresponse(res, 200, book);
                });
        } else {
            console.log('No bookid specified');
            sendJSONresponse(res, 404, {
                "message": "No bookid in request"
            });
        }
    };

这就是我试图只返回带有给定标题 ID 的标题,但是当我使用邮递员测试它时这不起作用

    module.exports.tilesReadOne = function(req, res) {
        console.log('Finding book details', req.params);
        if (req.params && req.params.titleid) {
            Bok
                .findById(req.params.titleid)
                .exec(function(err, book) {
                    if (!book) {
                        sendJSONresponse(res, 404, {
                            "message": "title not found"
                        });
                        return;
                    } else if (err) {
                        console.log(err);
                        sendJSONresponse(res, 404, err);
                        return;
                    }
                    console.log(book);
                    sendJSONresponse(res, 200, book);
                });
        } else {
            console.log('No title specified');
            sendJSONresponse(res, 404, {
                "message": "No title in request"
            });
        }
    };

【问题讨论】:

标签: json node.js mongodb


【解决方案1】:

除了聚合之外,您还可以在应用程序级别过滤标题:

    Bok
        .findById(req.params.bookid)
        .exec(function(err, book) {
            if (book) {
                var matchingTitles = book.titles.filter(function(title){
                    return title._id == req.params.bookid
                })
                // return titles, if any
            }
        })

【讨论】:

  • 从上面的 JSON 我希望得到这样的东西(只有一个标题来自给定的 id){“title”:“Animal Farm”,“_id”:“58dd3f2701cc081056135dae”,“reviews ": [ { "author": "Barry", "rating": 3, "reviewText": "这是一篇评论", "_id": "58dd8e13876c0f16b17cd7dc", "createdOn": "2017-03-30T23:00: 35.662Z" } ], "favouredBy": [ "bb, aa, cc" ] }
  • book.titles 是一个数组。它可能包含超过 1 个具有请求 ID 的标题。如果您愿意返回第一个匹配项,则返回过滤后数组的第一个元素,或者使用 for 循环迭代标题以退出第一个匹配项的迭代以对其进行优化。
猜你喜欢
  • 2018-06-23
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
相关资源
最近更新 更多