简体   繁体   English

流星从路线(restivus)访问事物

[英]Meteor accessing things from route (restivus)

Cannot understand why it restrict me accessing Meteor.userId() from route with following error: 无法理解为什么它会限制我从路由访问Meteor.userId()并出现以下错误:

Error: Meteor.userId can only be invoked in method calls. 错误:只能在方法调用中调用Meteor.userId。 Use this.userId in publish functions. 在发布函数中使用this.userId。 at AccountsServer.userId (packages/accounts-base/accounts_server.js:82:13) at Object.Meteor.userId (packages/accounts-base/accounts_common.js:223:19) at Object.get [as action] (server/main.js:15:28) at Route.share.Route.Route._callEndpoint (packages/nimble_restivus/lib/route.coffee:147:25) at packages/nimble_restivus/lib/route.coffee:59:33 at packages/simple_json-routes.js:98:9 在Object.get [作为操作](服务器)在AccountsServer.userId(packages / accounts-base / accounts_server.js:82:13)在Object.Meteor.userId(packages / accounts-base / accounts_common.js:223:19) /main.js:15:28)在Route.share.Route.Route._callEndpoint(packages / nimble_restivus / lib / route.coffee:147:25)在package / nimble_restivus / lib / route.coffee:59:33在package /simple_json-routes.js:98:9

It looks like i forget about publishing something.. but : 看来我忘了发布东西了..但是:

** You've set up some data subscriptions with Meteor.publish(), but **您已经使用Meteor.publish()设置了一些数据订阅,但是

** you still have autopublish turned on. **您仍然具有自动发布功能。 Because autopublish is still 因为自动发布仍然

** on, your Meteor.publish() calls won't have much effect. **,您的Meteor.publish()调用不会产生太大影响。 All data 所有资料

** will still be sent to all clients. **仍将发送给所有客户。

Very strange, how do you resolve this issue ? 很奇怪,您如何解决此问题? I'm new with meteor, but i don't have (google too) any idea how resolve this issue. 我是新来的流星,但我也没有(谷歌)如何解决此问题的任何想法。

Code server/main.js 代码服务器/main.js

   import { Meteor } from 'meteor/meteor'


Meteor.startup(() => {

      var Api = new Restivus({
        apiPath: 'api/',
        useDefaultAuth: true,
        prettyJson: true
      });

      Api.addRoute('test', {
        get: function () {
            console.log(this.userId())
            return {}
          }
      });
    });

this is not the Meteor context when you're inside Restivus. 当您在Restivus内部时, this不是Meteor上下文。 So you need to make it explicit: 因此,您需要使其明确:

Meteor.startup(() => {

      var self = this;  //<--- here explicitly declare this

      var Api = new Restivus({
        apiPath: 'api/',
        useDefaultAuth: true,
        prettyJson: true
      });

      Api.addRoute('test', {
        get: function () {
            console.log(self.userId())  //<-- use it like this
            return {}
          }
      });
    });

It were difficult day trying solve unsolvable things. 那天很难解决无法解决的事情。 I don't know what to think about it, maybe it is my fault trying work with meteor like with another tool. 我不知道该怎么想,也许这是我像其他工具一样尝试使用流星的错误。 As Pierre have said : 正如皮埃尔所说:

You can't authenticate a user if the client is not talking over DDP which is really bad when you want to provide API endpoints to your customers. 如果客户端未通过DDP进行对话,则无法对用户进行身份验证,这在您要向客户提供API终结点时确实很糟糕。

and, looks like he is right. 而且,看起来他是对的。 I'm just trying to work with this tool in wrong way. 我只是想以错误的方式使用此工具。 But i'd say, meteor have to place quote from Pierre to the main page inside of h1 tag at center of page, i'm sure here is a lot of people trying to make unsupported things work with non appropriate tools. 但是我要说,流星必须将Pierre的报价单放在页面中心的h1标签内的主页上,我敢肯定,有很多人试图使不支持的东西使用不合适的工具来工作。

Yeah, it even looks funny.... today ) 是的,它甚至看起来很有趣。。。。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM