当我尝试在MongoDB中保存信息时如何修复Cast失败

时间:2019-04-02 01:36:53

标签: node.js express mongoose ejs

我正在使用MongoDB设置新服务器,我尝试使用ejs模板保存信息并表达路由,但是当我尝试出现一些错误时。

这是我的错误,我将性别模式放在另一个文件js中,因为由于我将要撰写几篇不同性别的文章,因此无法在一个单独的文件中创建它来制作一个之后过滤?

   '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' }

我想做的是保存信息并在正面显示它

模型文件

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

控制器文件

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

ejs文件

<% include partials/header %>
</div>

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

<% include partials/footer %>```

0 个答案:

没有答案