krax1337
@krax1337
личинка Девопса

Как исправить ошибку Converting circular structure to JSON при выводе всех элементов коллекции из MongoDB, а также отедельных полей через mongoose?

Хочу вернуть прост вернуть определенные объекты из колекции, но это не получается сделать. Делал все как в примере.

Код ошибки:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ReplSet'
    |     property 's' -> object with constructor 'Object'
    |     property 'coreTopology' -> object with constructor 'ReplSet'
    |     ...
    |     property 's' -> object with constructor 'Object'
    --- property 'topology' closes the circle


Мой GET метод:

router.get('/assignments', auth.required, function (req, res, next) {
  var user = User.findById(req.payload.id);

  if (!user) { return res.sendStatus(401); }

  var assignments = Assignment.find({ author: user });

  return res.json({
    assignments: assignments.map(function (assignment) {
      return assignment.toJSONFor(user);
    })
  })
})


Метод формирования JSON в модели Mongoose

AssignmentSchema.methods.toJSONFor = function(user){
  return {
    slug: this.slug,
    publicSlug: this.publicSlug,
    title: this.title,
    description: this.description,
    createdAt: this.createdAt,
    updatedAt: this.updatedAt,
    attempts: this.attempts,
    questions: this.questions,
    author: this.author.toProfileJSONFor(user)
  }
};


Есть ли у вас какие-нибудь идеи, как можно это упростить или избавиться от ошибки?
  • Вопрос задан
  • 256 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
SummerWeb Ярославль
от 120 000 до 180 000 ₽
Brightdata Тель-Авив
от 5 500 до 6 500 $
Market-place Ростов-на-Дону
от 100 000 до 200 000 ₽
20 июн. 2024, в 17:08
3000 руб./в час
21 июн. 2024, в 22:53
1000 руб./за проект
21 июн. 2024, в 22:12
5000 руб./за проект