问题描述
我在尝试添加 pdf 文件生成时看到奇怪的行为.
以下代码在 if 语句中抛出:两者outes.js
Router.onBeforeAction(function () { if (!Meteor.user() || Meteor.loggingIn()) {this.redirect('welcome.view');} 别的 {Meteor.call("userFileDirectory", function (error, result) {如果(错误)抛出错误;别的控制台日志(结果);});this.next();} }, { 除外:['welcome.view'] });
错误:Meteor.userId 只能在方法调用中调用.利用发布函数中的 this.userId.在 Object.Meteor.userId(packages/accounts-base/accounts_server.js:19:1) 在 Object.Meteor.user(packages/accounts-base/accounts_server.js:24:1) 在 [object对象].Router.onBeforeAction.except(app/both/3-router/routes.js:10:15) 在包/铁:路由器/lib/router.js:277:1 在 [对象Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)在 [object Object].hookWithOptions(packages/iron:router/lib/router.js:276:1) at boundNext(packages/iron:middleware-stack/lib/middleware_stack.js:251:1) 在runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1) 在包/流星/dynamics_nodejs.js:121:1 在 [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";控制台日志(文件路径);var fs = Npm.require('fs');var data = fs.readFileSync(filePath);this.response.write(data);this.response.end();}, {其中:服务器"});
上面的代码工作正常;当我取出 onBeforeAction 代码时,pdf 被渲染到屏幕上并且没有抛出异常.
反之亦然,如果我取出服务器路由,则没有路由导致异常.
出现这种情况是因为您使用的路由是服务器端路由.Meteor 用于对用户进行身份验证的技术是通过 DDP 协议通过 websockets 完成的.
当您的浏览器向服务器发出 GET
/POST
请求时,它没有关于用户身份验证状态的任何信息.
您在 Route.onBeforeAction
中使用了 Meteor.user()
,但它无法访问此信息.
对此的解决方案是找到一种替代方法来验证用户.其中一种方法是使用 cookie.
这是 Meteor 身份验证系统的已知问题,请参阅:https://github.com/EventedMind/iron-router/issues/649
I'm seeing strange behavior when trying to add pdf file generation.
The following code, on the if statement, throws:bothoutes.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'] });
Only when I add this code into the file, and the /pdf route is taken:
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'
});
The above code works fine; the pdf is rendered to the screen and no exception is thrown, when I take out the onBeforeAction code.
The opposite is also true, if I take out the server route, there is no route that causes an exception.
This occurs because the route you're using is a server side route. The technique Meteor uses to authenticate a user is done via the DDP protocol, over websockets.
When your browser makes a GET
/POST
request to the server it doesn't have any information regarding the user's authentication state.
You use Meteor.user()
in your Route.onBeforeAction
but it has no access to this information.
The solution to this is find an alternative way to authenticate the user. One such method is to use cookie's.
This is known issue with Meteor's authentication system, see: https://github.com/EventedMind/iron-router/issues/649
这篇关于使用服务器路由和 onBeforeAction 时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!