2017-05-01 57 views
0

我試圖執行mdg:validated-methodinsert一個新集合,但是,我的代碼返回undefined錯誤消息。 run後面的console.log(profileCandidate)返回該對象。它似乎停止在insert工作。已驗證方法觸發並返回未定義錯誤

路徑:imports/api/profileCandidate/methods.js

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

import ProfileCandidate from './profileCandidate.js'; 

export const insertProfileCandidate = new ValidatedMethod({ 
    name: 'profileCandidate.insert', 
    validate: new SimpleSchema({ 
    'firstName': { type: String }, 
    }).validator(), 
    run(profileCandidate) { 
    console.log("profileCandidate", profileCandidate); 

    ProfileCandidate.insert({ 
     userId: Meteor.userId(), 
     createdAt: new Date(), 
     name: { 
     first: profileCandidate.firstName, 
     }, 
    }); 
    }, 
}); 

路徑:imports/api/profileCandidate/profileCandidate.js

import { Meteor } from 'meteor/meteor'; 
import { Mongo } from 'meteor/mongo'; 
import { check } from 'meteor/check'; 
import SimpleSchema from 'simpl-schema'; 

export const ProfileCandidate = new Mongo.Collection('profileCandidate'); 

ProfileCandidate.schema = new SimpleSchema({ 
    userId: { 
    type: String, 
    }, 
    createdAt: { 
    type: Date, 
    }, 
    name: Object, 
    'name.first': String, 
}); 

ProfileCandidate.attachSchema(ProfileCandidate.schema); 
+0

爲簡單模式包SimpleSchema.debug = true啓用調試模式,有幫助嗎? –

+0

沒有錯誤出現。就好像'ProfileCandidate.insert'不會觸發一樣。 – bp123

回答

0

原來有沒有在該方法的錯誤。錯誤在於我如何導入模式。我需要將import ProfileCandidate from './profileCandidate.js';更改爲import { ProfileCandidate } from './profileCandidate.js';。如果任何人可以闡明爲什麼你需要大括號,在這種情況下,我將不勝感激。

+1

如果您已經從模式文件中完成'導出默認常量insertProfileCandidate',那麼您可以使用不帶'{} –