Error: Mi promela es que al momento de salvar el libro en la base de datos (se guarda pero no hace referencia ni al author ni al genre.
'Cast to Embedded failed for value "Techno-thriller" at path "genre"',
name: 'CastError',
stringValue: '"Techno-thriller"',
kind: 'Embedded',
value: 'Techno-thriller',
path: 'genre',
reason: [MongooseError] } },
_message: 'Books validation failed',
name: 'ValidationError' }
controller.createBook const create = (req, res, next) => {
const name =if (!req.body.name;
) {
const author =return reqres.bodystatus(400).author;json({
const genre =error: req.body.genre;
'No data',
const numberInStock = req.body.numberInStock;});
}
const imagebook = new Books(req.body.image;);
letbook.save((err, newBookresult) ==> {
name: name,
if author:(err) author,{
genre:return genre,res.status(400).json({
numberInStockerror: numberInStockerr.message,
image: image,
});
Books.create(newBook, (err, newlyCreated) => {}
ifreturn res.status(err201) .json({
message: 'Book created console.log(err);succesfully',
} else {result,
res.redirect('/books/home/1'});
});
});
const mongoose = require('mongoose');
const { genreSchema }mongoose = require('./genres''mongoose');
const { authorSchema } = require('./author');
const booksSchemabookSchema = new mongoose.Schema({
name: {
name type: String,
},
author: {
type: mongoose.Schema.ObjectId,
typeref: authorSchema'Author',
},
numberInStock: {
numberInStock type: Number,
imagedefault: String0,
},
genreimage: {
type: String,
typedefault: genreSchema'/path/to/default/image.png',
},
}); genre: {
// const Books = type: mongoose.model('Book'Schema.ObjectId, booksSchema);
// exports.booksSchema = booksSchema;
// exports.Booksref: ='Genre', Books;
},
});
module.exports = mongoose.model('Books', booksSchemabookSchema);