我正在创建一个使用Mongoose和Express创建RESTful API的Node.js应用。 Mongoose模式定义了必需的字段;但是,当我通过邮递员提交空POST时,API会创建空记录。为什么Mongoose / MongoDB不能根据我的架构阻止保存?
我刚学习node.js / express / mongoose / mongodb并且来自mySQL / MSSQL世界,所以我可能会以错误的方式考虑架构必需字段(例如数据库约束)
这是我的路由器:
// Initialize controller(s) for CRUD
var customer = require('../controllers/customercontroller');
// Initialize Router
var express = require('express');
var router = express.Router();
router.get('/:id', function (req, res) {
console.log("Customer Requested");
customer.read(req, res);
});
router.get('/', function (req, res) {
console.log("Customer List Requested");
customer.getList(req, res);
});
router.put("/:id", function (req, res) {
console.log("Updating Customer");
customer.update(req, res);
});
router.post("/", function (req, res) {
console.log("Creating Customer");
customer.create(req, res);
});
router.delete("/:id", function (req, res) {
console.log("Delete Customer");
customer.delete(req, res);
});
module.exports = router;
这是我的控制器:
var Customer = require('../models/customermodel');
exports.getList = function (req, res) {
Customer.find({}, function (err, customer) {
if (err)
res.send(err);
res.json(customer);
});
};
exports.create = function (req, res) {
var customer = new Customer(req.body);
customer.save(function (err, customer) {
if (err)
res.send(err);
res.json(customer);
});
};
exports.read = function (req, res) {
Customer.find(req.params.id, function (err, customer) {
if (err)
res.send(err);
res.json(customer);
});
};
exports.update = function (req, res) {
Customer.findOneAndUpdate(req.params.id, function (err, customer) {
if (err)
res.send(err);
res.json(customer);
});
};
exports.delete = function (req, res) {
Customer.remove({_id: req.params.id}, function (err, customer) {
if (err)
res.send(err);
res.json({ message: 'Customer deleted'});
});
};
这是我的模特:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/hs_db');
var Schema = mongoose.Schema;
var CustomerSchema = new Schema({
"name": { type: String, Required: 'Required: name' },
"alias": { type: String, Required: 'Required: alias' },
"email": { type: String, Required: 'Required: email' },
"address1": { type: String, Required: 'Required: address1' },
"address2": { type: String, Required: 'Required: address2' },
"address3": { type: String, Required: 'Required: address3' },
"city": { type: String, Required: 'Required: city' },
"state": { type: String, Required: 'Required: state' },
"postcode": { type: String, Required: 'Required: postcode' },
"country": { type: String, Required: 'Required: country' },
"created": { type: Date, Required: 'Required: date' },
});
module.exports = mongoose.model('customer', CustomerSchema);
当我使用Postman发布空身并使用mongo控制台查询结果时:
> db.customers.find()
{ "_id" : ObjectId("591f25ae0cca8f5148cce551"), "__v" : 0 }
{ "_id" : ObjectId("591f25d40cca8f5148cce552"), "__v" : 0 }
{ "_id" : ObjectId("591f25f00cca8f5148cce553"), "__v" : 0 }
{ "_id" : ObjectId("591f2764dc5a191fe8730ccd"), "__v" : 0 }
>
所以,我的问题:
该模式是否应该验证数据并阻止类似于mySQL或MSSQL中的数据库约束的保存?或者模式是否只为node.js控制器提供数据引用,要求我创建业务逻辑来强制执行约束?
答案 0 :(得分:2)
架构属性名称区分大小写,因此将Required
字段更改为required
:
var CustomerSchema = new Schema({
"name": { type: String, required: 'Required: name' },
"alias": { type: String, required: 'Required: alias' },
...
答案 1 :(得分:0)
以简单的方式,您可以按如下方式定义必填字段:
var CustomerSchema = new Schema({
"name": { type: String, required: true },
"alias": { type: String, required: true },
...