【问题标题】:Meteor : Changes in collections are not reflected in rendered HTMLMeteor:集合中的更改不会反映在呈现的 HTML 中
【发布时间】:2015-09-29 23:34:45
【问题描述】:

我一直在尝试在 Meteor 中进行一些非常简单的发布/订阅,但我无法通过阅读可用的资源(例如 Meteor 文档)使其正常工作。

我正在使用装有 OSX Yosemite 的 Macbook Pro 并运行 Node.js v0.10.40、Meteor v1.1.0.2 和 Chrome 版本 43.0.2357.132(64 位)。

以下是我对两个不同示例的体验:

第一:简单的待办事项教程。

simple-todos.html

<head>
  <title>Todo List</title>
</head>

<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="task">
  <li>{{text}}</li>
</template>

simple-todos.js

Tasks = new Meteor.Collection("tasks");

// simple-todos.js
if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: function(){
      Tasks.find({});
    }
  });
}

问题描述

本教程指出,当向Tasks 添加项目时,它应该在浏览器中实时反映。它不是。我尝试在 Chrome 的 JS 控制台和 meteor shell 中添加项目为 Tasks.insert({text: "todo from server", createdAt: new Date()})。我还使用meteor mongo 添加了项目,但呈现的客户端视图仍然没有变化

安装了自动发布包,我可以在浏览器中从 JS 控制台插入和查询 Tasks 集合,但更改不会反映在呈现的 HTML 中。

第二个:一个简单的发布/订阅场景

基本.html

<head>
  <title>basic</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>
  <p>text gets pushed</p>
</body>

基本.js。

MessageColl = new Mongo.Collection("messages");

if(Meteor.isServer){
  Meteor.publish("messages",function(){
    MessageColl.find({});
  })
}

if(Meteor.isClient){
  Meteor.subscribe("messages", {
    onReady: function () { console.log("onReady And the Items actually Arrive", arguments); },
onError: function () { console.log("onError", arguments); }
  });
}

问题描述

当自动发布包添加到我的项目中时,一切都按预期工作。我可以在 Chrome 中从 JS 控制台插入新项目,也可以查询 MessageColl 集合并检索结果。

删除自动发布包后,我可以从 Chrome 中的 JS 控制台将项目插入到 MessageColl 集合中,并通过查询 meteor shell 中的集合来验证它们是否已添加。但是,当我尝试使用MessageColl.findOne()MessageColl.find().fetch() 查询时,返回值为undefined

在 HTML 文档结构中进行的所有更改都会按预期推送。

onReadyonError 回调函数都没有被调用,这指向了与订阅方法相关的问题。

【问题讨论】:

    标签: javascript mongodb meteor minimongo


    【解决方案1】:

    我认为这两个问题都很容易解决(但是当您无法弄清楚原因时会令人沮丧 - 我曾经去过那里)。基本上,您实际上并没有从函数中返回任何内容,因此您的代码没有结果。

    在您的模板助手中,您需要像这样添加return

    tasks: function(){
      return Tasks.find({});
    }
    

    同样,在您的出版物中,您还需要像这样的return

    Meteor.publish("messages",function(){
      return MessageColl.find({});
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多