首页 文章

尽管架构“需要”,但是Mongoose保存空记录

提问于
浏览
0

我正在创建一个使用Mongoose和Express创建RESTful API的Node.js应用程序 . Mongoose模式定义了必需的字段;但是,当我通过邮递员提交空POST时,API会创建空记录 . 为什么Mongoose / MongoDB不能阻止基于我的架构的保存?

我只是在学习node.js / express / mongoose / mongodb并且来自mySQL / MSSQL世界所以我可能会以错误的方式思考模式所需的字段(即像数据库约束)

Here is my router:

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

Here is my controller:

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

And here is my model:

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

When I post an empty body 使用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 }
>

SO, my question:

是不是模式应该验证数据并阻止类似于mySQL或MSSQL中的数据库约束的保存?或者模式是否只为node.js控制器提供数据引用,要求我创建业务逻辑来强制执行约束?

2 回答

  • 2

    架构属性名称区分大小写,因此将 Required 字段更改为 required

    var CustomerSchema = new Schema({
        "name": { type: String, required: 'Required: name' },
        "alias": { type: String, required: 'Required: alias' },
        ...
    
  • 0

    以简单的方式,您可以定义必填字段,如下所示:

    var CustomerSchema = new Schema({
        "name": { type: String, required: true },
        "alias": { type: String, required: true },
        ...
    

相关问题