我一直在寻找一个很好的解决方案,以解决我一直以来一直找不到的任何问题。基本上,我试图将引号添加到我一直在写的书博客中,但是在将引号保存到书中时遇到了一些麻烦。
我的报价模式如下:
var quoteSchema = mongoose.Schema({
text: String,
pageNum: String
});
我的书本模式如下:
var bookSchema = new mongoose.Schema({
title: String,
image: String,
author: String,
yearWritten: String,
quotes: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Quote"
}
],
favorite: Boolean
});
应该将报价实际创建和保存到书中的路线如下:
router.post("/", function(req, res) {
// Look up book using ID
Book.findById(req.params.id, function(err, book) {
if(err) {
console.log(err);
res.redirect("/books");
} else {
// Create new quote
Quote.create(req.body.quote, function(err, quote) {
if(err) {
console.log(err);
res.redirect("/books");
} else {
// Save quote
quote.save();
// Add new quote to book
book.quotes.push(quote);
// Save the book
console.log(book.quotes);
book.save(); // THIS IS THE ERROR!
// console.log(quote);
// console.log(book);
// Redirect to book show page
res.redirect("/books/" + book._id);
}
});
}
});
});
我得到的错误是:
(node:5606) UnhandledPromiseRejectionWarning: ValidationError: Book validation failed: quotes: Cast to [undefined] failed for value "[{"_id":"5e8c
e6fc6ced1d075192ac21"}]" at path "quotes"
我很确定这与保存书有关,但是我不确定如何解决。任何帮助表示赞赏!