Skip to main content
mejora en la pregunta
Origen Enlace

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);

Error:

'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 = (req, res) => {
    const name = req.body.name;
    const author = req.body.author;
    const genre = req.body.genre;
    const numberInStock = req.body.numberInStock;
    const image = req.body.image;
    let newBook = {
        name: name,
        author: author,
        genre: genre,
        numberInStock: numberInStock,
        image: image,
    };
  Books.create(newBook, (err, newlyCreated) => {
      if (err) {
          console.log(err);
      } else {
          res.redirect('/books/home/1');
      }
  });
const mongoose = require('mongoose');
const { genreSchema } = require('./genres');
const { authorSchema } = require('./author');
const booksSchema = new mongoose.Schema({
    name: String,
    author: {
        type: authorSchema,
    },
    numberInStock: Number,
    image: String,
    genre: {
        type: genreSchema,
    },
});
// const Books = mongoose.model('Book', booksSchema);
// exports.booksSchema = booksSchema;
// exports.Books = Books;
module.exports = mongoose.model('Books', booksSchema);

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.

    const create = (req, res, next) => {
  if (!req.body) {
    return res.status(400).json({
      error: 'No data',
    });
  }
  const book = new Books(req.body);
  book.save((err, result) => {
    if (err) {
      return res.status(400).json({
        error: err.message,
      });
    }
 

    return res.status(201).json({
      message: 'Book created succesfully',
      result,
 
    });
  });
};
   const mongoose = require('mongoose');

const bookSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  author: {
    type: mongoose.Schema.ObjectId,
    ref: 'Author',  
  },
  numberInStock: {
    type: Number,
    default: 0,
  },
  image: {
    type: String,
    default: '/path/to/default/image.png',
  },
  genre: {
    type: mongoose.Schema.ObjectId,
    ref: 'Genre',  
  },
});


module.exports = mongoose.model('Books', bookSchema);
se añadieron 482 caracteres en el cuerpo
Origen Enlace

view.ejs*:

 <div class="container h-100">
    <div class="row">
        <div class="col-lg-6 col-md-offset-3">
            <h1>Add a New Book</h1>
            <form action="/books/new/create" method="post">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="book_name"> Book Name: </label>
                            <input type="text"  class="form-control" name="name">
                        </div>
                         <div class="form-group">
                            <label for="author_name">Author Name: <for="exampleFormControlSelect1">Author</label>
                            <input type="text" <select class="form-control" name="author"> name="<%= author._id %>">
                            <% author.forEach(function(authors){ %>
                            <option><%= authors._id %></option>
                            <% }); %>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="genre">Bookfor="exampleFormControlSelect1">Genre</label>
                            <select class="form-control"  name="<%= genre:._id <%>">
                            <% genre.forEach(function(genres){ %>
                            <option><%= genres.name %></label>option>
                            <input<% type="text"}); %>
 class="form-control" name="genre">                          </select>
                        </div>
                        <div class="form-group">
                            <label for="numberInStock">Number in stock: </label>
                            <input type="number"  class="form-control" name="numberInStock">
                        </div>
                        <div class="form-group">
                            <label for="image">Image: </label>
                            <input type="text"  class="form-control" name="image">
                        </div>
                        <button type="submit" class="btn btn-success">Success</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

view.ejs*:

 <div class="container h-100">
    <div class="row">
        <div class="col-lg-6 col-md-offset-3">
            <h1>Add a New Book</h1>
            <form action="/books/new/create" method="post">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="book_name"> Book Name: </label>
                            <input type="text"  class="form-control" name="name">
                        </div>
                        <div class="form-group">
                            <label for="author_name">Author Name: </label>
                            <input type="text"  class="form-control" name="author">
                        </div>
                        <div class="form-group">
                            <label for="genre">Book genre: </label>
                            <input type="text"  class="form-control" name="genre">
                        </div>
                        <div class="form-group">
                            <label for="numberInStock">Number in stock: </label>
                            <input type="number"  class="form-control" name="numberInStock">
                        </div>
                        <div class="form-group">
                            <label for="image">Image: </label>
                            <input type="text"  class="form-control" name="image">
                        </div>
                        <button type="submit" class="btn btn-success">Success</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

view.ejs:

 <div class="container h-100">
    <div class="row">
        <div class="col-lg-6 col-md-offset-3">
            <h1>Add a New Book</h1>
            <form action="/books/new/create" method="post">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="book_name"> Book Name: </label>
                            <input type="text"  class="form-control" name="name">
                        </div>
                         <div class="form-group">
                            <label for="exampleFormControlSelect1">Author</label>
                            <select class="form-control"  name="<%= author._id %>">
                            <% author.forEach(function(authors){ %>
                            <option><%= authors._id %></option>
                            <% }); %>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="exampleFormControlSelect1">Genre</label>
                            <select class="form-control"  name="<%= genre._id %>">
                            <% genre.forEach(function(genres){ %>
                            <option><%= genres.name %></option>
                            <% }); %>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="numberInStock">Number in stock: </label>
                            <input type="number"  class="form-control" name="numberInStock">
                        </div>
                        <div class="form-group">
                            <label for="image">Image: </label>
                            <input type="text"  class="form-control" name="image">
                        </div>
                        <button type="submit" class="btn btn-success">Success</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
se añadieron 1861 caracteres en el cuerpo
Origen Enlace

view.ejs*:

 <div class="container h-100">
    <div class="row">
        <div class="col-lg-6 col-md-offset-3">
            <h1>Add a New Book</h1>
            <form action="/books/new/create" method="post">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="book_name"> Book Name: </label>
                            <input type="text"  class="form-control" name="name">
                        </div>
                        <div class="form-group">
                            <label for="author_name">Author Name: </label>
                            <input type="text"  class="form-control" name="author">
                        </div>
                        <div class="form-group">
                            <label for="genre">Book genre: </label>
                            <input type="text"  class="form-control" name="genre">
                        </div>
                        <div class="form-group">
                            <label for="numberInStock">Number in stock: </label>
                            <input type="number"  class="form-control" name="numberInStock">
                        </div>
                        <div class="form-group">
                            <label for="image">Image: </label>
                            <input type="text"  class="form-control" name="image">
                        </div>
                        <button type="submit" class="btn btn-success">Success</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

view.ejs*:

 <div class="container h-100">
    <div class="row">
        <div class="col-lg-6 col-md-offset-3">
            <h1>Add a New Book</h1>
            <form action="/books/new/create" method="post">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="book_name"> Book Name: </label>
                            <input type="text"  class="form-control" name="name">
                        </div>
                        <div class="form-group">
                            <label for="author_name">Author Name: </label>
                            <input type="text"  class="form-control" name="author">
                        </div>
                        <div class="form-group">
                            <label for="genre">Book genre: </label>
                            <input type="text"  class="form-control" name="genre">
                        </div>
                        <div class="form-group">
                            <label for="numberInStock">Number in stock: </label>
                            <input type="number"  class="form-control" name="numberInStock">
                        </div>
                        <div class="form-group">
                            <label for="image">Image: </label>
                            <input type="text"  class="form-control" name="image">
                        </div>
                        <button type="submit" class="btn btn-success">Success</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
Se reemplaza snippet por codigo en linea, se corrigen algunos errores ortográficos y gramaticales. Se cambia el título por algo más representativo del problema
Origen Enlace
Loading
Origen Enlace
Loading