【问题标题】:Meteor publish/subscribe not working流星发布/订阅不起作用
【发布时间】:2018-12-26 15:35:57
【问题描述】:

我正在使用流星 1.7,我的发布/订阅只返回空数组。 文件结构:

-all/
  -shared.js
-client/
  -main.js
-imports/
  -Elsewhere.js
-server/
  -main.js

shared.js:

Chats = new Mongo.Collection('chats')

客户端/main.js:

Template.main.onCreated(()=>{
  Meteor.subscribe('chats')
});

服务器/main.js

Meteor.publish('chats', function(){
  console.log(Chats.find().fetch()) //Shows that I have documents in the collection
  return Chats.find();
});

Elsewhere.js

Template.Elsewhere.helpers({
  chats(){
    console.log(Chats.find().fetch()) //[]
    return Chats.find().fetch()
  }
})

为什么我没有得到我正在发布的内容?

--------------------------新东西-------- --------------------------

我现在不确定这是加载顺序问题、反应性问题、发布/订阅问题还是它们的混合问题。我有这个sn-p

search(collection, where, id, part, callback){
  var result
  if(id){
    console.log(Meteor.users.find().fetch()) //[]
    result = Collections[collection].findOne(id)
  }else{
    result = Collections[collection].find(where ? where : {}, {many:true}).fetch()
  }
  if(result){
    if(callback){
      callback(result)
      return
    }else{
      if(part){
        return result[part]
      }else{
        return result
      }
    }
  }
}

我还注意到此日志的输出发生在我订阅之前。该文件位于 /imports/scripts/tools.js

【问题讨论】:

  • 也许你的助手在你的订阅被执行之前执行。尝试将您的订阅放在自动运行块中,如下所示:blazejs.org/guide/…
  • @GaëtanRouziès 不,没有变化

标签: meteor meteor-publications meteor-collections


【解决方案1】:

我意识到我没有为我的助手使用响应式数据源,我在调用 tool.search 函数时设置了一个 Session 变量,该函数只运行了一次。

【讨论】:

    【解决方案2】:

    在自动运行块中订阅“聊天”并检查处理程序是否准备就绪。然后找到并获取。

    this.autorun(() => {
        let handler = Meteor.subscribe('chats');
    
        if(handler.ready()) {
             console.log(Chats.find().fetch())
        }
      });
    

    【讨论】:

    • if(handler.ready()) 块内的任何内容都不会运行
    • 好的,我打错了,现在它运行了,我被订阅了...排序...客户端/main.js 中的日志正确记录,但我在其他任何地方仍然得到一个空数组在应用中
    • 在您的聊天集合中插入一些内容并检查更改
    • 行为和以前一样,在 main 中正确登录,在其他地方登录错误
    • 尝试使用跟踪器:stackoverflow.com/questions/28890672/…。同时删除 autopublish
    猜你喜欢
    • 2018-12-18
    • 2015-09-02
    • 1970-01-01
    • 2014-05-04
    • 2014-06-16
    • 2015-05-15
    • 1970-01-01
    相关资源
    最近更新 更多