在客户端代码中重用Mongoose模式

时间:2015-12-28 17:43:19

标签: angularjs mongoose meanjs

我正在开发一个MEANJS应用程序,并定义了一个Mongoose模式。现在我想在客户端代码中重用它来验证带有Mongoose in the browser的表单。

基本应用程序是使用yo meanjs生成的,我有一个包含模型的文件,如下所示:

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var schemaDefinition = { ... };
var schemaOptions = { ... };

var EventSchema = new Schema(schemaDefinition, schemaOptions);

mongoose.model('Event', EventSchema);

在客户端中包含该模式以将其重用于表单验证的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以指定validators directly to your schema。我假设Mongoose原型无法在客户端呈现,而且需要大量工作才能将其映射到Angular的验证方式。 此外,缺乏安全性仅在客户端验证。

从Angular应用程序中使用AJAX调用非常容易将您的模型序列化并将其解析为Express监听器。 由于您的架构具有验证器,因此不会将格式错误的数据解析到您的集合中。