2017-05-20 8 views
0

データベースに書き込もうとすると、自分のスキーマ(enums、minなどの形式のフィールドの制約が適用されない)に問題があります。 recipes.js:スキーマの検証、受け入れられない制約

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

var schema = new Schema({ 
     name: {type:String, required:true},  
     category_name: [{ type: String, enum: [ "breakfast", "lunch","dinner"], required:true }], 
     difficulty_level: { type: String, enum: [ 'easy', 'medium', 'hard'], required:true }, 
     time_of_prep: { type: String, enum: [ '<=30 min', '1h-2h', 'more than 2h'], required:true }, 
     no_of_servings: { type: Number, min:1 , required:true},  
     ingredients_specified: [{ 
      ingredient: {type: String, required:true }, 
       amount: {type: String, required:true} 
     }], 
     description: {type:String, required:true}, 
     guide: {type:String, required:true} 
    }); 

    module.exports = mongoose.model('Recipe',schema); 

recipesSeeder.js:

var Recipe = require('../models/recipes'); 
var mongoose = require('mongoose'); 
mongoose.Promise = require('bluebird'); 
mongoose.connect('pathtodb'); 
var conn = mongoose.connection; 

var recipes=[ 
    new Recipe({ 
     name: 'TestName1', 
     category_name: 'lunch', 
     difficulty_level: 'easy', 
     time_of_prep: '<=30min', 
     no_of_servings: 5, 
     ingredient_specified: { 
      ingredient:'banana', 
      amount:'2 pieces' 
     }, 
     description: 'description1', 
     guide: 'guide1' 
    }), 
    new Recipe({ 
     name: ' ', 
     category_name: 'testingerrorcat', 
     difficulty_level: 'testingerrordiff', 
     time_of_prep: 'testingerrortime', 
     no_of_servings: 0, 
     ingredient_specified: { 
      ingredient:'ingredient2', 
      amount:'amount2' 
     }, 
     description: 'description2', 
     guide: 'guide2' 

    }), 
    new Recipe({ 
     name: 'TestName3', 
     category_name: 'lunch', 
     difficulty_level: 'easy', 
     time_of_prep: '<=30min', 
     no_of_servings: 5, 
     ingredient_specified: { 
      ingredient:'ingredient3', 
      amount:'amount3' 
     }, 
     description: 'description3', 
     guide: 'guide3' 
    }) 
]; 

var done = 0; 
for (var i = 0; i < recipes.length; i++){ 

    conn.collection('recipes2').save(recipes[i]); 
    done++; 
    if(done === recipes.length){ 
       console.log('data inserted'); 
       exit(); 
      } 
} 

function exit() { 
    mongoose.disconnect(); 
} 

答えて

0

mongoose documentation検証によると、エラーiwthつのオブジェクトを作成したが、それは何も言わdoesntのとちょうど間違っdata.belowを挿入することは私のコードですミドルウェアであり、preとして登録されています( 'save')フックはデフォルトですべてのスキーマに適用されます。つまり、初期化時ではなく、その中でsaveを呼び出そうとしているときに検証が適用されます。また、モデル自体ではなくconn.collection( 'recipes2')のsaveを呼び出すので、検証は行われませんでした。

適切な方法は、コールバックを返すモデルでvalidate関数を呼び出すことです。エラーのコールバックチェックでは、エラーがなければモデルに保存を呼び出してデータベースに格納することができます。

このように、forループをfor-eachに置き換えました。ここで

recipes.forEach(function (recipe) { 
    recipe.validate(function(err) { 
     if (err) { 
      console.log(err);//don't save, do something with the error 
      return; 
     } 
     console.log("validation passsed"); 
     recipe.save(); 
    }); 

}); 

は、それがで動作する必要のあるコレクションを知っているように、私は自分のスキーマに行った変更です。

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

var schema = new Schema({ 
     name: {type:String, required:true},  
     category_name: [{ type: String, enum: [ 'breakfast', 'lunch','dinner'], required:true }], 
     difficulty_level: { type: String, enum: [ 'easy', 'medium', 'hard'], required:true }, 
     time_of_prep: { type: String, enum: [ '<=30min', '1h-2h', 'more than 2h'], required:true }, 
     no_of_servings: { type: Number, min:1 , required:true},  
     ingredients_specified: [{ 
      ingredient: {type: String, required:true }, 
       amount: {type: String, required:true} 
     }], 
     description: {type:String, required:true}, 
     guide: {type:String, required:true} 
    }); 

    module.exports = mongoose.model('Recipe',schema, 'recipes2'); //ADDED COLLECTION NAME HERE 

これは、この後、私は自分のスキーマ内にマイナーな問題に遭遇し、言われて、あなたが定義した「< = 30分」を持っているが、しかし、あなたのオブジェクトであなたはスペースのない「< = 30分」を持っています。これは小さな問題です。

私があなたのコードで見る別の問題は、あなたのforループで、asyncというsaveを呼び出すことです。あなたのコードはsaveを呼び出すでしょうが、それが完了するのを待つことはなく、単に移動します。 これは、あなたがあなたのループ全体で完了し、実際にデータベースにまだ保存されていない間に、exit関数を呼び出して接続を閉じることができることを意味します。

私はそれが役に立ちそうです。

関連する問題