【发布时间】:2018-11-01 09:21:15
【问题描述】:
我在Node.js 中使用Express 创建了一个HTTP API,用于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所以它被执行了那个而不是我想要的。
【问题讨论】: