Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 流星从路径访问物体(restivus)_Meteor - Fatal编程技术网

Meteor 流星从路径访问物体(restivus)

Meteor 流星从路径访问物体(restivus),meteor,Meteor,无法理解为什么它会限制我从路由访问Meteor.userId(),并出现以下错误: 错误:Meteor.userId只能在方法调用中调用。在发布函数中使用this.userId。在Object.Meteor.userId(packages/accounts base/accounts_common.js:223:19)在Object.get[as action](server/main.js:15:28)在Route.share.Route.Route.\u callEndpoint(packa

无法理解为什么它会限制我从路由访问Meteor.userId(),并出现以下错误:

错误:Meteor.userId只能在方法调用中调用。在发布函数中使用this.userId。在Object.Meteor.userId(packages/accounts base/accounts_common.js:223:19)在Object.get[as action](server/main.js:15:28)在Route.share.Route.Route.\u callEndpoint(packages/nimble_restivus/lib/Route.coffee:147:25)在packages/nimble_restivus/lib/route.coffee:59:33在packages/simple_json-routes.js:98:9

看来我忘了发布一些东西。。但是:

**您已经使用Meteor.publish()设置了一些数据订阅,但是

**您仍然打开了“自动发布”。因为自动发布仍然是

**在上,您的Meteor.publish()调用不会产生太大影响。所有数据

**仍将发送到所有客户端

很奇怪,你怎么解决这个问题?我是meteor的新手,但我(谷歌也是)不知道如何解决这个问题

代码服务器/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 {}
          }
      });
    });

当您在Restivus中时,这不是Meteor上下文。因此,您需要明确说明:

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 {}
          }
      });
    });
流星启动(()=>{
var self=this;//这一天很难解决无法解决的问题。我不知道该怎么想,也许是我的错,尝试使用meteor就像使用其他工具一样。如前所述:

如果客户端没有通过DDP进行对话,则无法对用户进行身份验证。当您希望向客户提供API端点时,DDP非常糟糕

而且,看起来他是对的。我只是试图以错误的方式使用这个工具。但我要说的是,meteor必须将Pierre的引语放在页面中心h1标记的主页内,我相信这里有很多人试图使用不合适的工具使不受支持的东西工作


是的,它甚至看起来很有趣……今天)

这与发布无关,你根本无法访问
Meteor.userId()
那里,使用
this.userId
,因为它返回相同的值,但不起作用。类型错误:Object[Object Object]没有方法“userId”,你导入了Restivus吗?它不在你的代码中。导入{Restivus}来自'meteor/nimble:restivus';它是可用的,不需要导入就可以正常工作。但这并不重要(见下面我的答案)。不,它不工作。TypeError:Object[Object global]在Object.get[as action]中没有方法“userId”(server/main.js:15:26)