Я пытаюсь / POST элементов в базу данных MongoDB, но только три из пяти вещей внутри, или когда, когда флаг «required: true» включен, он получает ошибку проверки.
сервер / модели / item.js
var mongoose = require('mongoose');
var Item = mongoose.model('Item', {
category: {
type: String,
minlength: 1,
trim: true,
required: true,
},
product: {
title: {
type: String,
minlength: 1,
trim: true,
required: true
},
quantity: {
type: Number,
default: 1,
required: true
},
added: {
type: Date,
default: Date.now
},
description: {
type: String,
minlength: 1,
trim: true,
required: true
}
}
});
module.exports = {Item};
сервер / db / mongoose.js
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/Website', { useNewUrlParser: true });
module.exports = {mongoose};
server / server.js
const _ = require('lodash');
const express = require('express');
const bodyParser = require('body-parser');
const {ObjectID} = require('mongodb');
const hbs = require('hbs');
const fs = require('fs');
var {mongoose} = require('./db/mongoose');
var {Item} = require('./models/item');
const port = process.env.PORT || 3000;
var app = express();
// console.log(__dirname);
app.use(bodyParser.json());
hbs.registerPartials(__dirname + '/../views/partials')
app.set('view engine', 'hbs');
app.use((req, res, next) => {
var now = new Date().toString();
var log = `${now}: ${req.method} ${req.url}`;
console.log(log);
fs.appendFile('server.log', log + '\n', (err) => {
if (err) {
console.log('Unable to append to server.log.');
}
});
next();
});
app.use(express.static(__dirname + '/../public'));
hbs.registerHelper('getCurrentYear', () => {
return new Date().getFullYear()
});
// POST ROUTES
app.post('/equipment', (req, res) => {
console.log("Post command received");
console.log("This is the category: " + req.body.category);
console.log("This is the title: " + req.body.product.title);
console.log("This is the quantity: " + req.body.product.quantity);
console.log("This is the description: " + req.body.product.description);
var item = new Item({
category: req.body.category,
title: req.body.product.title,
quantity: req.body.product.quantity,
description: req.body.product.description
});
console.log(item);
item.save().then((doc) => {
console.log(doc);
res.send(doc);
}, (e) => {
res.status(400).send(e);
});
});
app.all('*', function(req, res) {
res.render("error.hbs");
});
app.listen(port, () => {
console.log(`Started on port ${port}`);
});
module.exports = {app};
Наконец, то, что я пытаюсь / POST к / оборудованию через Почтальон
{
"category": "Lighting",
"product": {
"title": "Litepanel Duo Traveller Kit",
"quantity": 1,
"description": "A two light LED panel kit, containing one spot and one flood Litepanel 1x1 panel, with all drop in filters needed. Incldues two stands, power leads and adaptors, v-lock battery plates, and carry case."
}
}
При удаленииТег «required: true» в item.js, POST работает, но «product.title» и «product.description» не передаются в БД.
Любая помощь приветствуется.