2015-07-11 58 views
2

我一直在嘗試做一些非常簡單的發佈/訂閱流星,我無法讓它工作,因爲我期望從閱讀可用的源如Meteor文檔。流星:集合中的變化並不反映在呈現的HTML

我正在使用OSX Yosemite的Macbook Pro,並運行Node.js v0.10.40,Meteor v1.1.0.2和Chrome版本43.0.2357.132(64位)。

以下是我與兩個不同的例子經歷:

一:簡單的待辦事項教程。

簡單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> 

簡單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。

二:一個簡單的發佈/訂閱方案

Basic.html

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

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

Basic.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回調函數,也不會指向與訂閱方法相關的問題。

回答

1

我認爲這兩個問題都相當直接地解決(但當你不能解決原因 - 我去過那裏時會感到沮喪)。基本上,你實際上並沒有從你的函數中返回任何東西,所以你的代碼沒有結果。

在模板助手,你需要添加return這樣的:

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

同樣,在您的出版物還需要return這樣的:

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