我有一个在server / methods / methods.js中定义的方法:
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);
}
},
我在server / config / 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 :(得分:1)
server/config/roles.js
在server/methods/methods.js
之前进行评估(它们具有相同的嵌套级别,' config'按字母顺序排列'方法'),因此,方法在通话时没有定义。一个简单的解决方案就是在Meteor.startup
回调中调用该方法:
Meteor.startup(function () {
roles.forEach(function(role) {
if (!Meteor.roles.find({name: role}).count()) {
Meteor.call('createRole', role);
}
});
});
这将确保在执行roles
代码之前评估所有文件。替代解决方案包括在lib
目录中移动方法定义或将其添加到包中。
答案 1 :(得分:0)
尝试在Meteor.startup中运行它
Meteor.startup(function () {
roles.forEach(function(role) {
if (!Meteor.roles.find({name: role}).count()) {
Meteor.call('createRole', role);
}
});
});