Я создал HTTP API, используя Express
в Node.js
для операций CRUD.Это работает частично, но когда я делаю запрос GET
, появляется ошибка: TypeError: Converting circular structure to JSON
.Другие методы HTTP (например, POST
и DELETE
) работают.
Это моя модель:
const mongoose = require('mongoose');
const coment = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
text: {type: String, required: true},
author_coment: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
date: {type: Date, default: Date.now}
});
const vote = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
author_vote: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
vote: {type: Boolean, required: true},
date: {type: Date, default: Date.now}
})
const book = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
title: {type: String, required: true},
author: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
sinopsis: {type: String, required: true},
text: {type: mongoose.Schema.Types.ObjectId, ref: 'Text'},
creation_date: {type: Date, default: Date.now},
cover: {type: String},
coments: [coment],
votes: [vote]
});
module.exports = mongoose.model('Book', book);
Это моя GET
функция:
// [GET: Book info]
router.get('/info/:book_id', function (req, res) {
Book.findById(req.params.book_id, (err, book) => {
if (err) return res.status(500).send(err);
res.status(200).send(book);
});
});
Вот моя модель пользователя:
const mongoose = require('mongoose');
const user = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: {type: String, required: true},
email: {type: String, required: true},
password: {type: String, required: true}
});
module.exports = mongoose.model('User', user);
Редактировать:
После некоторого копания я выяснил, в чем проблема, у меня была другая функция с таким URL: /: skip/: talk
, поэтомувыполнил это вместо того, что я хотел.