嘗試添加pdf文件生成時出現奇怪的行爲。使用服務器路由和onBeforeAction時出現異常
下面的代碼,在if語句,拋出: 兩個\ routes.js
Router.onBeforeAction(function() { if (!Meteor.user() || Meteor.loggingIn()) {
this.redirect('welcome.view'); } else {
Meteor.call("userFileDirectory", function (error, result) {
if (error)
throw error;
else
console.log(result);
});
this.next(); } }, { except: ['welcome.view'] });
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.Meteor.user (packages/accounts-base/accounts_server.js:24:1) at [object Object].Router.onBeforeAction.except (app/both/3-router/routes.js:10: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)
只有當我這段代碼添加到該文件,和/ PDF路線採取:
Router.route('/pdf', function() {
var filePath = process.env.PWD + "/server/.files/users/test.pdf";
console.log(filePath);
var fs = Npm.require('fs');
var data = fs.readFileSync(filePath);
this.response.write(data);
this.response.end();
}, {
where: 'server'
});
上面的代碼工作正常;當我取出onBeforeAction代碼時,PDF將呈現在屏幕上,並且不會引發異常。
反過來也是如此,如果我拿出服務器路由,沒有路由導致異常。
我在這裏閱讀你的答案:http://stackoverflow.com/questions/16727922/access-meteor-userid-from-outside-a-method-publish,我現在可以使用這個過程(寫在' 13)?如果是這樣,我會將Cookie檢查代碼放在onBeforeAction方法或服務器端路由中嗎? – Aaron
我想你回答了爲什麼,如何解決這個問題由David Weldon在這裏回答:http://stackoverflow.com/questions/27714618/server-side-routes-in-iron-router-and-meteor – Aaron
什麼我意思是說,問題是關於例外,並且每個David Weldon的答案都是例外,但是服務器端認證系統/方法仍然需要解決(這是您答案的一部分,但沒有完全回答這個問題)...再次,你確實回答了爲什麼會發生這種情況,但僅以身份驗證而言。 – Aaron