Meteor Users集合和Deps.自动运行的问题

Meteor Users collection and Deps.autorun problems

本文关键字:运行 问题 Deps Users 集合 Meteor      更新时间:2023-09-26

我仍然在努力理解如何访问流星。Users作为另一个集合查询的外键。我知道默认情况下只有当前用户被发布,所以我在服务器上有一个发布作为

Meteor.publish('itemOwner', function(userId) {
  check(userId, String);
  var user = Meteor.users.find({id: userId});
  return user;
  // return Meteor.users.find({id: userId}, {
  //   fields: {'profile': 1} });
});

我有一个Deps。客户端自动运行…

Deps.autorun(function () {
  var itemOwnerId = Session.get("itemOwnerID");
  if (itemOwnerId) {
    debugger
    var ID = Session.get("itemOwnerID");
    Meteor.subscribe('itemOwner', Session.get("itemOwnerID"));
  }
});

我在模态表单加载上设置会话ID,并通过调用ownerProfile helper(或尝试)在模板中显示它

Template.showQuoteModalInner.helpers({
  getQuote: function () {
    // Get the quote ID from the session var
    var quote = Session.get("quoteID");
    if(quote) {
      debugger;
      var ID = quote.user._id;
      Session.set("itemOwnerID", quote.user._id);
      return quote;
    }
  },
  ownerProfile: function() {
    debugger;
    var quote = Session.get("quoteID");
    if(quote) {
      var ID = quote.user._id;
      var theUser = Meteor.users.find({_id: quote.user._id});
      return theUser; 
    };
  }
});

现在,我可以在每个阶段跟踪用户ID,并看到它被正确地传递给自动运行程序和帮助程序。如果我在ownerProfile helper中的调试器处停止程序,并在控制台中放入Meteor.user。fetch({_id: "这里的id "}).fetch()我得到正确的用户返回…但是,在处理程序本身Meteor.users.find返回null??我错过了什么?

我注意到两种可能性。

首先,在publish函数的find中缺少下划线。

.find({id: userId})应该是.find({_id: userId})

但是,如果您在控制台中看到用户(而不是登录用户),则这可能不是问题。

第二,如果你没有从你的Template.showQuoteModalInner.ownerProfile助手看到用户,这可能是因为你正在返回一个find()而不是findOne()。

find()返回游标,而findOne()返回记录。如果您想要显示单个用户的属性,请尝试使用findOne()。