Pregunta

I need to display data to all clients using Meteor.publish/subscribe.I did one sample example in that example what i am did is 10 records are inserted at that time of server startup and these 10 records are trying to display to all clients.The Problem is The data doesn't shows the clients.I didn't have any idea because i am new to meteor JS.So Please see the below code and suggest me how to do?

HTML Code :

<head>
  <title>DataApp</title>
</head>

<body>
  {{> main}}
</body>

<template name="main">

         {{#each messages}}      
              {{messages}}
        {{/each}}
</template>

ANd JS Code is :

Messages = new Meteor.Collection("messages");
if (Meteor.isClient) 
{
 Meteor.subscribe('messages');

}

if (Meteor.isServer) 
{
  Meteor.startup(function ()
  {
    // code to run on server at startup
    for(var i = 0 ; i <= 10 ; i++ )
    {
      console.log("EXE"+i);
      Messages.insert({ name: "EXE"+i });
     }

  });


     Meteor.publish('messages', function() {

           return  Messages.find();
        });
}
¿Fue útil?

Solución

Firstly, you are missing a template helper on your template main, so make your Meteor.isClient look like this:

if (Meteor.isClient) {
  Meteor.subscribe('messages');

  // this is the template helper that is referenced by {{messages}} 
  // in your template 'main' 
  Template.main.messages = function () {
    return Messages.find();
  };
}

Secondly, the handlebars in your html don't make sense, try this instead:

<template name="main">
    <!-- inserts value of property 'name' of each object in 'messages'  -->
    {{#each messages}}      
        {{name}}
    {{/each}}
</template>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top