2016-04-15 51 views
7

我將我的Meteor應用程序從Meteor 1.2遷移到Meteor 1.3,並按照http://guide.meteor.com/methods.html#validated-method上的指南創建驗證的方法。未找到流星驗證的方法

當我調用該方法時,我相信客戶端模擬正在發生,因爲我可以註銷到控制檯,但這總是跟着錯誤Method '...' not found

/imports/ui/pages/register.js

import {Meteor} from 'meteor/meteor'; 
import {Template} from 'meteor/templating'; 
import {FlowRouter} from 'meteor/kadira:flow-router'; 

// Methods 
import {createAccount} from '/imports/api/accounts/methods.js'; 

// HTML 
import './register.html'; 

Template.Register_page.events({ 
    'submit form': function(event) { 
    event.preventDefault(); 

    var user = { 
     email: $('#email').val(), 
     password: $('#password').val(), 
     profile: { 
     firstName: $('#firstName').val(), 
     lastName: $('#lastName').val() 
     } 
    }; 

    createAccount.call(user, function(err) { 
     if (err) { 
     console.error(err); 
     } else { 
     console.log('User successfully registered'); 
     FlowRouter.go('Dashboard'); 
     } 
    }); 
    } 
}); 

/imports/api/accounts/methods.js

import {Meteor} from 'meteor/meteor'; 
import {ValidatedMethod} from 'meteor/mdg:validated-method'; 
import {SimpleSchema} from 'meteor/aldeed:simple-schema'; 
import {Accounts} from 'meteor/accounts-base'; 

export const createAccount = new ValidatedMethod({ 
    name: 'createAccount', 
    validate: new SimpleSchema({ 
    email: { type: String }, 
    password: { type: String }, 
    profile: { type: Object }, 
    "profile.firstName": { type: String }, 
    "profile.lastName": { type: String } 
    }).validator(), 
    run(user) { 
    console.log(user); 
    Accounts.createUser(user); 
    }, 
}); 

客戶端控制檯

Object {email: "[email protected]", password: "testPassw0rd", profile: Object} methods.js:18 
errorClass {error: 404, reason: "Method 'createAccount' not found", details: undefined, message: "Method 'createAccount' not found [404]", errorType: "Meteor.Error"} register.js:28 

回答

9

我相信這不起作用的原因是因爲我沒有在啓動時初始化服務器上​​的JavaScript。

添加下面的固定問題:

/imports/startup/server/index.js

import './register-api.js'; 

/imports/startup/server/register-api.js

import '/imports/api/accounts/methods.js';