【发布时间】:2017-10-19 22:29:31
【问题描述】:
我正在创建一个使用 Mongoose 和 Express 来创建 RESTful API 的 Node.js 应用程序。 Mongoose 模式定义了必填字段;但是,当我通过 Postman 提交空 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 控制器提供数据来引用,需要我创建业务逻辑来强制执行约束?
【问题讨论】:
-
啊……就是这样!愚蠢的程序员错误 :) 感谢您的新眼睛!!!
标签: node.js mongodb express mongoose model