Collections 在模板帮助器中使用Meteor.users()

Collections 在模板帮助器中使用Meteor.users(),collections,meteor,meteor-blaze,meteor-helper,Collections,Meteor,Meteor Blaze,Meteor Helper,我在Meteor 0.8.0中从模板助手中获取用户配置文件数据时遇到问题。这段代码在以前的版本中运行良好,但自从今天早上升级后就被破坏了。我最初认为这是模板帮助程序运行两次的问题,但当我深入研究时,我发现问题比这更微妙 下面,模板助手“findClientLiason”被调用两次(它的输出在控制台中记录了两次)。第一次用户将显示为“未定义”,第二次正确的用户对象将以我期望的方式出现。两次“clientLiason”都将正确输出 对我来说最有趣的是,如果我删除'var user=Meteor.us

我在Meteor 0.8.0中从模板助手中获取用户配置文件数据时遇到问题。这段代码在以前的版本中运行良好,但自从今天早上升级后就被破坏了。我最初认为这是模板帮助程序运行两次的问题,但当我深入研究时,我发现问题比这更微妙

下面,模板助手“findClientLiason”被调用两次(它的输出在控制台中记录了两次)。第一次用户将显示为“未定义”,第二次正确的用户对象将以我期望的方式出现。两次“clientLiason”都将正确输出

对我来说最有趣的是,如果我删除'var user=Meteor.users.findOne({u id:clientLiason});'调用以获取findOne调用助手只调用一次

在我看来,对Meteor.users集合的调用迫使对数据库进行另一次调用。而且它第一次称之为Meteor.users集合是空的

我的出版物和订阅如下所示。我正在使用Iron Router global waitOn()函数,但我想知道Meteor.users集合是否应该更早加载

任何想法都将不胜感激。再次感谢

publications.js

Meteor.publish('allUsers', function() {
    return Meteor.users.find();
});
Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    waitOn: function() { 
        return [    
            Meteor.subscribe('clientsActive'),
            Meteor.subscribe('allUsers'),
            Meteor.subscribe('notifications')
        ];
}
});
Template.clientItem.helpers({
    findClientLiason: function(clientLiason) {
        var user = Meteor.users.findOne({_id: clientLiason});
        console.log(clientLiason);
        console.log(user);
        return user.profile.name;
    }
});
router.js

Meteor.publish('allUsers', function() {
    return Meteor.users.find();
});
Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    waitOn: function() { 
        return [    
            Meteor.subscribe('clientsActive'),
            Meteor.subscribe('allUsers'),
            Meteor.subscribe('notifications')
        ];
}
});
Template.clientItem.helpers({
    findClientLiason: function(clientLiason) {
        var user = Meteor.users.findOne({_id: clientLiason});
        console.log(clientLiason);
        console.log(user);
        return user.profile.name;
    }
});
clientItem.html

<template name="clientItem">
    {{findClientLiason clientLiason}}
</template>

这是有意义的,因为首先在页面加载且用户集合为空时呈现模板,然后在数据更改时重新呈现模板(例如,本地mongo集合已填充)

您应该编写模板,以期望在页面加载时无数据开始。我会将助手更改为以下内容:

findClientLiaison: function(clientLiaison) {
  var user = Meteor.users.findOne(clientLiaison);
  if (user) {
    return user.profile.name;
  }
  return "no data yet";
}

好的,看起来它已经修好了。非常感谢。不过有几个问题。这是否随着Blaze的发布而改变?另外,像这样重复两次的代码是否有问题,或者这只是它工作方式的一个函数。再次感谢。您一直都必须注意初始加载状态,但Blaze改变了助手的工作方式,因此这可能与此有关。@yankeyhotel为什么使用
findOne
而不是
find
?看到这个了吗