【问题标题】:In what circumstances is Meteor.user() not reactive?在什么情况下 Meteor.user() 没有反应?
【发布时间】:2014-08-26 05:18:28
【问题描述】:

我有一个模板助手

  multiple_emails: ->
    Meteor.user().emails.length > 1

在 emails 数组中添加或删除对象时不会重新运行。为什么是这样?我应该如何编写这个帮助程序以使其重新运行?

【问题讨论】:

  • 一开始,用户不会登录,Meteor.user() 将评估为null,它没有emails 属性。您没有收到任何错误消息表明这一点?
  • 使用快速渲染,所以通常不会。

标签: meteor


【解决方案1】:

代码中的其他地方有些东西不起作用。

我给你做了一个小垫子:http://meteorpad.com/pad/B6L3cCXAPwPSdqc5s

模板:

<head>
  <title>Leaderboard</title>
</head>

<body>
  {{> loginButtons}}
  {{#if currentUser}}
   {{> addEmail}}
  {{/if}}
</body>

<template name="addEmail">
  <button>addEmail</button>
  nb: {{emails}}
</template>

客户端JS:

Template.addEmail.events({
  'click button': function() {
    Meteor.users.update({
      _id: Meteor.userId()
    }, {
      $addToSet: {
        emails: {address: 'email' + Math.random()}
      }
    })
  }
});

Template.addEmail.helpers({
  emails: function() {
    return Meteor.user().emails.length;
  }
});

您需要帐户库、密码和 ui 包。

使用电子邮件注册并单击添加电子邮件按钮。如您所见,当单击按钮时,它会更新数字(并很快回到 1,因为您无法以这种方式更新它,但这只是为了提出一般性观点)。

【讨论】:

  • 故事的寓意:检查您的控制台输出,那里有一个错误需要您修复。
【解决方案2】:

大多数情况下,当集合在客户端似乎没有反应时,发布/订阅部分的某处存在问题/错误。

【讨论】:

  • 我仔细查看了我的发布功能并发现了问题,我不知道发布功能中的其他代码不是反应式的。所以在我的例子中,来自 Mongo 查询的结果被用于发布查询,当然这个结果永远不会改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 2021-10-18
  • 1970-01-01
  • 2014-01-25
  • 2011-03-28
  • 1970-01-01
相关资源
最近更新 更多