javascript - locomotivejs 中服务器端的模型验证

标签 javascript express locomotivejs

我想知道是否有检查我的模型在服务器端是否有效的功能,我在 express 上使用 locomotivejs。

这是我的模型模式

var BillSchema = new Schema({
   BrandId:Schema.Types.ObjectId,
    ModelNo:{ type: String, required: true },
    BillNo:{ type: String, required: true },
    years:Number,
    months:Number,
    })

在我的 Controller 中,我从 View 中接收值。

var bill=new Bill();
  bill.CategoryId=self.param('CategoryId');
  bill.BrandId=self.param("BrandId");
  bill.ModelNo=self.param("Model");
  bill.BillNo=self.param("BillNo");
if(bill.categoryId!="" && bill.BrandId!="" && bill.ModelNo!="" && bill.years!="")
{
// code to save the values to db
}

有没有办法避免这种验证

if(bill.categoryId!="" && bill.BrandId!="" && bill.ModelNo!="" && bill.years!="")

在 .Net MVC 中使用类似 ModelState.Isvalid 的东西

提前致谢...

最佳答案

伙计们,我的问题已经解决了。 mongoose为此提供了validate函数。

bill.validate(function (err) {
  if(err) {// handle error}
else
 {// save to db.}

谢谢...

关于javascript - locomotivejs 中服务器端的模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355049/

相关文章:

javascript - 循环对象的对象

javascript - 将项目数组传递给 jquery 函数

javascript - 使用云函数将函数值添加到 firestore 中

node.js - 简单 express 应用程序 'Cannot GET/'

javascript - 在javascript中摆脱对象的所有属性的最快方法是什么?

node.js 和 express : use more than one views directory

node.js - 如何使用 mongoose 在 mongodb 中搜索具有动态字段的数据?

node.js - 在 nginx 后面使用expressjs设置响应头

javascript - 如何在 Locomotive Js 中设置 CoffeeScript?