【问题标题】:Exception from sub books id dMBTck6CaSupWbEna TypeError: Cannot read property ‘emails’ of null来自子书 id dMBTck6CaSupWbEna TypeError 的异常:无法读取 null 的属性“电子邮件”
【发布时间】:2018-05-19 19:47:23
【问题描述】:

每当用户登录他的帐户时,我都会收到异常,并且在刷新页面之前我看不到集合数据...... **[但如果我像这样使用它,我不会收到任何异常

Books.find({“customer_name”:this.userId);

而不是

Books.find({“customer_name”:Meteor.user().emails[0].address);

我不需要这一步,因为在我的应用中,用户集合的查找应该基于他们的电子邮件 ID,而不是用户 ID...]** 我的代码如下所示: 有人,请帮帮我:frowning_face:

服务器 - main.js:

var My_collection;
Meteor.publish(‘books’, function() {
My_collection = 
Books.find({“customer_name”:Meteor.user().emails[0].address);
return My_collection;

客户端-main.js:

Template.viewBooks.helpers({
books() {
return Books.find({}, { sort: { books_order: -1 } }).fetch();
},
});

客户端 - main.html

{{#each books}}
{{books_id}} 
{{/each}}

错误:

应用程序运行在:http://localhost:3000/I20171205-16:06:49.773(5.5)? 来自子事务 id wQ9DpjjABEiPzMZ9P TypeError 的异常:不能 读取 null I20171205-16:06:49.872(5.5) 的属性“电子邮件”?在 Subscription._handler (server/main.js:16:75) I20171205-16:06:49.873(5.5)?在 MaybeAuditArgumentChecks (包/ddp-server/livedata_server.js:1768:12) I20171205-16:06:49.873(5.5)?在 DDP.CurrentPublicationInvocation.withValue (包/ddp-server/livedata_server.js:1043:15) I20171205-16:06:49.873(5.5)?在 Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1134:15) I20171205-16:06:49.873(5.5)?在 Subscription.runHandler (包/ddp-server/livedata_server.js:1041:51) I20171205-16:06:49.873(5.5)?在 包/ddp-server/livedata_server.js:826:41 I20171205-16:06:49.874(5.5)?在函数..each..forEach (packages/underscore.js:147:22) I20171205-16:06:49.874(5.5)?在 包/ddp-server/livedata_server.js:822:9 I20171205-16:06:49.874(5.5)?在 Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1134:15) I20171205-16:06:49.874(5.5)?在 Session._setUserId (packages/ddp-server/livedata_server.js:816:34)

【问题讨论】:

    标签: meteor


    【解决方案1】:

    在服务器上使用 Meteor.user() 通常是不好的做法。如果没有足够的信息来说明您如何发布和订阅您的收藏集,并且您说使用 this.userId 时没有收到任何错误,您可以通过以下方式找到电子邮件地址:

    var currentUser = Meteor.users.findOne(this.userId);
    var userEmail = currentUser.emails[0].address;
    

    然后在 Books.find 中使用它

    如果您能提供有关订阅和发布的更多信息,我可以提供更多帮助

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多