2017-05-31 1 views
1

$ setを使用してMongoDBコレクションを更新しようとしています。コレクション内のフィールドは存在しません。フィールドを追加しようとすると、コレクションはデータを一時的に保存し、データが消えてエラーClientError: name.first is not allowed by the schemaを返します。私はここで間違って何をしているのか分からず、私は数時間Googleを検索してきました。シンプルスキーマが更新されない

私が使用している:

  • 流星
  • シンプルスキーマ(NPM)
  • 流星-コレクション2コア

パス:Simple-Schema

const ProfileCandidateSchema = new SimpleSchema({ 
    userId: { 
    type: String, 
    regEx: SimpleSchema.RegEx.Id, 
    }, 
    createdAt: { 
    type: Date, 
    }, 
    name: { type: Object, optional: true }, 
    'name.first': { type: String }, 
}); 

パス:Method.js

import { Meteor } from 'meteor/meteor'; 
import { ProfileCandidate } from '../profileCandidate'; 
import SimpleSchema from 'simpl-schema'; 
import { ValidatedMethod } from 'meteor/mdg:validated-method'; 

export const updateContactDetails = new ValidatedMethod({ 
    name: 'profileCandidate.updateContactDetails', 

validate: new SimpleSchema({ 
    'name.first': { type: String }, 
    }).validator({ clean: true }), 

run(data) { 
    ProfileCandidate.update({userId: this.userId}, 
    {$set: 
     { 
     name: {'first': "Frank"}, 
     } 
    }, (error, result) => { 

    if(error) { 
     console.log("error: ", error) 
    } 
}); 
} 
}); 

UPDATE

パス:call function

updateContactDetails.call(data, (error) => { 
    if (error) { 
    console.log("err: ", error); 
      console.log("err.error: ", error.error); 
    } 
}); 

答えて

1

はあなたが交換してみてください:

validate: new SimpleSchema({ 
    'name.first': { type: String }, 
}).validator({ clean: true }), 

Method.js中で:

validate: new SimpleSchema({ 
    name: { 
    type: new SimpleSchema({ 
     first: String, 
    }), 
    optional: true, 
}).validator({ clean: true }), 
+0

これは機能しませんでした。エラー 'メソッド' profileCandidate.updateContactDetails 'を呼び出す際の例外ClientError:name.firstはスキーマによって許可されていません。 at Array.forEach'を呼び出します。上記のProfileCandidate.callを追加します。 – bp123

+0

それはあなたが欲しいものですか? – bp123

+1

それはうまくいくようです。私は実際にバリデータ関数を理解していません。 – bp123

関連する問題