通过物化路径从mongoose返回异步数据,返回重复数据

时间:2016-01-20 00:38:31

标签: javascript node.js asynchronous

我在解析json时遇到问题。我正在使用mongoose-path-tree来跟踪物化路径的嵌套注释。但是,由于某种原因,它会返回重复数据。

router.get('/posts/:post', function (req, res, next) {
  req.post.populate('comments', function (err, post) {
    var rootComments = post.comments.filter(function(comment) {
      return comment.path.indexOf('#') === -1;
    });
    var nestedComments = [];
    async.each(rootComments, function(comment, callback) {
      comment.getChildrenTree(function(err, children) {
        if (children) {
        nestedComments.push(children);
        }
        callback(null);
      });
    }, function(err) {
      if (err) {
        console.log(err);
      } else {
        res.json(nestedComments);
      }
    });
});
});

预期产出:

  [
    {
      "_id": "569aa69c516be0a920e8b42d",
      "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d",
      "parent": "569aa61102ae935620b432b9",
      "post": "5661f6806b174dc07aae36b9",
      "body": "Yur gay",
      "author": "jonjon",
      "dislikes": [
        "ron"
      ],
      "likes": [
        "ron",
        "smitty",
        "jack"
      ],
      "date": "2016-01-16T20:22:52.474Z",
      "__v": 0,
      "children": [
        {
          "_id": "569aa69c516be0a920e8b42e",
          "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e",
          "parent": "569aa69c516be0a920e8b42d",
          "post": "5661f6806b174dc07aae36b9",
          "body": "sukkkkkkkkkkka",
          "author": "jon",
          "dislikes": [
            "ron"
          ],
          "likes": [
            "ron",
            "smitty",
            "jack",
            "poe"
          ],
          "date": "2016-01-16T20:22:52.478Z",
          "__v": 0,
          "children": [
            {
              "_id": "569aa69c516be0a920e8b42f",
              "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e#569aa69c516be0a920e8b42f",
              "parent": "569aa69c516be0a920e8b42e",
              "post": "5661f6806b174dc07aae36b9",
              "body": "Yur gayer",
              "author": "moe",
              "dislikes": [
                "ron"
              ],
              "likes": [
                "ron",
                "jack"
              ],
              "date": "2016-01-16T20:22:52.478Z",
              "__v": 0,
              "children": []
            },
            {
              "_id": "569aa69c516be0a920e8b430",
              "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e#569aa69c516be0a920e8b430",
              "parent": "569aa69c516be0a920e8b42e",
              "post": "5661f6806b174dc07aae36b9",
              "body": "hahahah",
              "author": "roe",
              "dislikes": [
                "ron"
              ],
              "likes": [
                "ron",
                "smitty",
                "jack",
                "preemie"
              ],
              "date": "2016-01-16T20:22:52.478Z",
              "__v": 0,
              "children": [
                {
                  "_id": "569aa69c516be0a920e8b431",
                  "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e#569aa69c516be0a920e8b430#569aa69c516be0a920e8b431",
                  "parent": "569aa69c516be0a920e8b430",
                  "post": "5661f6806b174dc07aae36b9",
                  "body": "lol",
                  "author": "ronnie",
                  "dislikes": [
                    "ron"
                  ],
                  "likes": [
                    "ron",
                    "smitty",
                    "jack"
                  ],
                  "date": "2016-01-16T20:22:52.479Z",
                  "__v": 0,
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]

实际输出:

[
  [
    {
      "_id": "569aa65169a0b188204692d7",
      "path": "569aa61102ae935620b432b9#569aa65169a0b188204692d7",
      "parent": "569aa61102ae935620b432b9",
      "post": "5661f6806b174dc07aae36b9",
      "body": "Yur gay",
      "author": "jonjon",
      "dislikes": [
        "ron"
      ],
      "likes": [
        "ron",
        "smitty",
        "jack"
      ],
      "date": "2016-01-16T20:21:37.447Z",
      "__v": 0,
      "children": [
        {
          "_id": "569aa65169a0b188204692d8",
          "path": "569aa61102ae935620b432b9#569aa65169a0b188204692d7#569aa65169a0b188204692d8",
          "parent": "569aa65169a0b188204692d7",
          "post": "5661f6806b174dc07aae36b9",
          "body": "sukkkkkkkkkkka",
          "author": "jon",
          "dislikes": [
            "ron"
          ],
          "likes": [
            "ron",
            "smitty",
            "jack",
            "poe"
          ],
          "date": "2016-01-16T20:21:37.451Z",
          "__v": 0,
          "children": [
            {
              "_id": "569aa65169a0b188204692d9",
              "path": "569aa61102ae935620b432b9#569aa65169a0b188204692d7#569aa65169a0b188204692d8#569aa65169a0b188204692d9",
              "parent": "569aa65169a0b188204692d8",
              "post": "5661f6806b174dc07aae36b9",
              "body": "Yur gayer",
              "author": "moe",
              "dislikes": [
                "ron"
              ],
              "likes": [
                "ron",
                "jack"
              ],
              "date": "2016-01-16T20:21:37.451Z",
              "__v": 0,
              "children": []
            },
            {
              "_id": "569aa65169a0b188204692da",
              "path": "569aa61102ae935620b432b9#569aa65169a0b188204692d7#569aa65169a0b188204692d8#569aa65169a0b188204692da",
              "parent": "569aa65169a0b188204692d8",
              "post": "5661f6806b174dc07aae36b9",
              "body": "hahahah",
              "author": "roe",
              "dislikes": [
                "ron"
              ],
              "likes": [
                "ron",
                "smitty",
                "jack",
                "preemie"
              ],
              "date": "2016-01-16T20:21:37.451Z",
              "__v": 0,
              "children": [
                {
                  "_id": "569aa65169a0b188204692db",
                  "path": "569aa61102ae935620b432b9#569aa65169a0b188204692d7#569aa65169a0b188204692d8#569aa65169a0b188204692da#569aa65169a0b188204692db",
                  "parent": "569aa65169a0b188204692da",
                  "post": "5661f6806b174dc07aae36b9",
                  "body": "lol",
                  "author": "ronnie",
                  "dislikes": [
                    "ron"
                  ],
                  "likes": [
                    "ron",
                    "smitty",
                    "jack"
                  ],
                  "date": "2016-01-16T20:21:37.452Z",
                  "__v": 0,
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "_id": "569aa69c516be0a920e8b42d",
      "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d",
      "parent": "569aa61102ae935620b432b9",
      "post": "5661f6806b174dc07aae36b9",
      "body": "Yur gay",
      "author": "jonjon",
      "dislikes": [
        "ron"
      ],
      "likes": [
        "ron",
        "smitty",
        "jack"
      ],
      "date": "2016-01-16T20:22:52.474Z",
      "__v": 0,
      "children": [
        {
          "_id": "569aa69c516be0a920e8b42e",
          "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e",
          "parent": "569aa69c516be0a920e8b42d",
          "post": "5661f6806b174dc07aae36b9",
          "body": "sukkkkkkkkkkka",
          "author": "jon",
          "dislikes": [
            "ron"
          ],
          "likes": [
            "ron",
            "smitty",
            "jack",
            "poe"
          ],
          "date": "2016-01-16T20:22:52.478Z",
          "__v": 0,
          "children": [
            {
              "_id": "569aa69c516be0a920e8b42f",
              "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e#569aa69c516be0a920e8b42f",
              "parent": "569aa69c516be0a920e8b42e",
              "post": "5661f6806b174dc07aae36b9",
              "body": "Yur gayer",
              "author": "moe",
              "dislikes": [
                "ron"
              ],
              "likes": [
                "ron",
                "jack"
              ],
              "date": "2016-01-16T20:22:52.478Z",
              "__v": 0,
              "children": []
            },
            {
              "_id": "569aa69c516be0a920e8b430",
              "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e#569aa69c516be0a920e8b430",
              "parent": "569aa69c516be0a920e8b42e",
              "post": "5661f6806b174dc07aae36b9",
              "body": "hahahah",
              "author": "roe",
              "dislikes": [
                "ron"
              ],
              "likes": [
                "ron",
                "smitty",
                "jack",
                "preemie"
              ],
              "date": "2016-01-16T20:22:52.478Z",
              "__v": 0,
              "children": [
                {
                  "_id": "569aa69c516be0a920e8b431",
                  "path": "569aa61102ae935620b432b9#569aa69c516be0a920e8b42d#569aa69c516be0a920e8b42e#569aa69c516be0a920e8b430#569aa69c516be0a920e8b431",
                  "parent": "569aa69c516be0a920e8b430",
                  "post": "5661f6806b174dc07aae36b9",
                  "body": "lol",
                  "author": "ronnie",
                  "dislikes": [
                    "ron"
                  ],
                  "likes": [
                    "ron",
                    "smitty",
                    "jack"
                  ],
                  "date": "2016-01-16T20:22:52.479Z",
                  "__v": 0,
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
]

0 个答案:

没有答案