使用服务器路由和onBeforeAction时出现异常 [英] Exception when using a server route and onBeforeAction

查看:147
本文介绍了使用服务器路由和onBeforeAction时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试添加pdf文件时看到了奇怪的行为。

I'm seeing strange behavior when trying to add pdf file generation.

以下代码在if语句中抛出:
both \ routes.js

The following code, on the if statement, throws: both\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'] });




错误:Meteor.userId只能在方法调用中调用。在发布函数中使用
this.userId。 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)

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路由时:

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'
});

上述代码正常;当我取出onBeforeAction代码时,pdf呈现在屏幕上并且没有抛出异常。

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.

推荐答案

这是因为您使用的路由是服务器端路由。 Meteor用于验证用户的技术是通过DDP协议通过websockets完成的。

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.

当您的浏览器生成 GET / POST 向服务器请求它没有关于用户身份验证状态的任何信息。

When your browser makes a GET/POST request to the server it doesn't have any information regarding the user's authentication state.

你在 Route.onBeforeAction 中使用 Meteor.user(),但它无法访问此信息。

You use Meteor.user() in your Route.onBeforeAction but it has no access to this information.

解决方法是找到另一种验证用户身份的方法。其中一种方法是使用cookie。

The solution to this is find an alternative way to authenticate the user. One such method is to use cookie's.

这是Meteor认证系统的已知问题,请参阅: https://github.com/EventedMind/iron-router/issues/649

This is known issue with Meteor's authentication system, see: https://github.com/EventedMind/iron-router/issues/649

这篇关于使用服务器路由和onBeforeAction时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆