不显示Mongo中的字段

时间:2019-07-16 09:41:53

标签: reactjs mongodb express schema

在制定了像这样的mongo方案之后,我遇到了以下问题:

let Books = new Schema({
    video_ru: String,
    name: {
        ru: String,
        uz: String,
        en: String
    },

只有字段video_ru进入数据库。 如果没有类别,那么一切都会正常进行。

这是我的路线

const express = require('express');
const booksRoutes = express.Router();

// Require books model in our routes module
let Books = require('./books.model');

// Defined store route
booksRoutes.route('/add').post(function (req, res) {
  let books = new Books(req.body);
  books.save()
    .then(books => {
      res.status(200).json({'books': 'books is added successfully'});
    })
    .catch(err => {
    res.status(400).send("unable to save to database");
    });
});

// Defined get data(index or listing) route
booksRoutes.route('/').get(function (req, res) {
    Books.find(function(err, bookses){
    if(err){
      console.log(err);
    }
    else {
      res.json(bookses);
    }
  });
});

// Defined edit route
booksRoutes.route('/edit/:id').get(function (req, res) {
  let id = req.params.id;
  Books.findById(id, function (err, books){
      res.json(books);
  });
});

//  Defined update route
booksRoutes.route('/update/:id').post(function (req, res) {
    Books.findById(req.params.id, function(err, books) {
    if (!books)
      res.status(404).send("data is not found");
    else {
        books.name.ru = req.body.name_ru;
        books.subname.ru = req.body.subname_ru;
        books.description.ru = req.body.description_ru;
        books.logo.ru = req.body.logo_ru;
        books.video_ru = req.body.video_ru;
        books.name.uz = req.body.name_uz;
        books.subname.uz = req.body.subname_uz;
        books.description.uz = req.body.description_uz;
        books.logo.uz = req.body.logo_uz;
        books.name.en = req.body.name_en;
        books.subname.en = req.body.subname_en;
        books.description.en = req.body.description_en;
        books.logo.en = req.body.logo_en;




        books.save().then(books => {
          res.json('Update complete');
      })
      .catch(err => {
            res.status(400).send("unable to update the database");
      });
    }
  });
});

// Defined delete | remove | destroy route
booksRoutes.route('/delete/:id').get(function (req, res) {
    Books.findByIdAndRemove({_id: req.params.id}, function(err, books){
        if(err) res.json(err);
        else res.json('Successfully removed');
    });
});

module.exports = booksRoutes;

最后我在数据库中得到这个json [{“ _id”:“ 5d2d94ca6206e73ff02e920d”,“ video_ru”:“”,“ __ v”:0}]

但需要名称字段

1 个答案:

答案 0 :(得分:0)

好吧!您的架构是正确的。模式是嵌套的,因此您必须像下面那样保存值,或使用bodyparser将表单输入包装到正文中。

booksRoutes.route('/add').post(function (req, res) {
  let books = new Books(req.body);
  books.name = {
    ru: req.body.ru,
    uz: req.body.ru,
    en: req.body.ru
  };
  books.save()
    .then(books => {
      res.status(200).json({'books': 'books is added successfully'});
    })
    .catch(err => {
    res.status(400).send("unable to save to database");
    });
});