2015-04-26 59 views
0

我在服务器/方法/ methods.js定义的方法:为什么我的流星方法404

Meteor.methods({ 
    'createRole': function(name) { 
    if (this.connection && !Roles.userIsInRole(this.userId, 'manage-users')) { 
     throw new Meteor.Error('Not authorized to create user roles'); 
    } else if (!Meteor.roles.find({name: name}).count()) { 
     Roles.createRole(name); 
     var admins = Meteor.users.find({ emails: { $elemMatch: { address: { $in: _.pluck(Meteor.settings.admins, 'email')}}}}).fetch(); 
     return Roles.addUsersToRoles(admins, name); 
    } 
    }, 

我称之为服务器/配置/ roles.js方法:

var roles = [ 
    'manage-users', 
    'schedule-any', 
    'edit-any', 
    'delete-any', 
    'manage-settings', 
    'schedule-own', 
    'edit-own', 
    'delete-own' 
    ]; 

// Creates any roles in the list that don't exist 
roles.forEach(function(role) { 
    if (!Meteor.roles.find({name: role}).count()) { 
    Meteor.call('createRole', role); 
    } 
}); 

这会导致404跟踪以下堆栈跟踪:

W20150426-12:19:18.264(-4)? (STDERR) Error: Method not found [404] 
W20150426-12:19:18.264(-4)? (STDERR) at [object Object]._.extend.apply (/private/var/folders/21/_h470ps14cn22051frhwhrfr0000gn/T/meteor-test-runsw86fl/.meteor/local/build/programs/server/packages/ddp.js:2330:19) 
W20150426-12:19:18.264(-4)? (STDERR) at [object Object]._.extend.call (/private/var/folders/21/_h470ps14cn22051frhwhrfr0000gn/T/meteor-test-runsw86fl/.meteor/local/build/programs/server/packages/ddp.js:2300:17) 
W20150426-12:19:18.264(-4)? (STDERR) at Accounts.onCreateUser.email (app/server/config/roles.js:17:12) 
W20150426-12:19:18.264(-4)? (STDERR) at Array.forEach (native) 
W20150426-12:19:18.264(-4)? (STDERR) at app/server/config/roles.js:15:7 
W20150426-12:19:18.265(-4)? (STDERR) at app/server/config/roles.js:42:3 
W20150426-12:19:18.265(-4)? (STDERR) at /private/var/folders/21/_h470ps14cn22051frhwhrfr0000gn/T/meteor-test-runsw86fl/.meteor/local/build/programs/server/boot.js:222:10 
W20150426-12:19:18.265(-4)? (STDERR) at Array.forEach (native) 
W20150426-12:19:18.265(-4)? (STDERR) at Function._.each._.forEach (/Users/raddevon/.meteor/packages/velocity_meteor-tool/.1.1.3_2.mgkc7d++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11) 
W20150426-12:19:18.266(-4)? (STDERR) at /private/var/folders/21/_h470ps14cn22051frhwhrfr0000gn/T/meteor-test-runsw86fl/.meteor/local/build/programs/server/boot.js:117:5 
W20150426-12:19:18.266(-4)? (STDERR) 
=> Exited with code: 8 

为什么我无法调用此方法?我可以在server/startup/startup.js中调用其他方法,所以我很困惑,为什么我无法在这里调用这个特定的方法。

+0

“roles.js”中的代码是否在评估文件后立即运行,或者是否是回调的一部分? –

+0

@DavidWeldon它不在回调中。我应该在评估文件时运行。 – raddevon

回答

1

server/config/roles.js被评估,因此,该方法不在通话时定义。一个简单的解决方案只是调用的方法,在Meteor.startup回调:

Meteor.startup(function() { 
    roles.forEach(function(role) { 
    if (!Meteor.roles.find({name: role}).count()) { 
     Meteor.call('createRole', role); 
    } 
    }); 
}); 

这将确保所有文件之前已经执行roles代码进行评估。替代解决方案包括在lib目录内移动您的方法定义或将其添加到包中。

0

尝试在Meteor.startup运行之前server/methods/methods.js(它们具有嵌套的相同的水平和“配置”是按字母顺序之前“方法”)

Meteor.startup(function() { 
    roles.forEach(function(role) { 
     if (!Meteor.roles.find({name: role}).count()) { 
      Meteor.call('createRole', role); 
     } 
    }); 
});