我正在尝试使用Restivus公开get路由以按序列号返回特定文档。
基本代码:
// lib/router.js
Router.onBeforeAction(requireLogin, {
except: ['auth', 'api']
});
// server/restivus.js
Meteor.startup(function() {
Restivus.configure({
useAuth: false,
prettyJson: true
});
});
// lib/collection/schedule.js
if (Meteor.isServer) {
Restivus.addRoute('schedules/:serial', {authRequired: false}, {
get: function() {
var schedule = Schedules.findOne({collector: this.urlParams.serial});
if (schedule) {
return {status: 'success', data: schedule};
}
}
});
}
完成日志错误:
curl -X GET http://localhost:3000/api/schedules/00000020
Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
at Object.Meteor.userId (packages/accounts-base/accounts_server.js:19:1)
at [object Object].requireLogin (app/lib/router.js:85:15)
at packages/iron:router/lib/router.js:277:1
at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
at [object Object].hookWithOptions (packages/iron:router/lib/router.js:276:1)
at boundNext (packages/iron:middleware-stack/lib/middleware_stack.js:251:1)
at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)
at packages/meteor/dynamics_nodejs.js:121:1
at [object Object].dispatch (packages/iron:middleware-stack/lib/middleware_stack.js:275:1)
at [object Object].RouteController._runRoute (packages/iron:router/lib/route_controller_server.js:63:1)
答案 0 :(得分:1)
我解决了添加!Meteor.isServer挂钩onBeforeAction:
var requireLogin = function() {
if (!Meteor.isServer && !Meteor.userId() && !Meteor.loggingIn()) {
this.redirect('auth');
} else {
this.next();
}
};
Router.onBeforeAction(requireLogin, {
except: ['auth']
});