简体   繁体   中英

display records from mongodb meteorjs

I'm trying to display the records from a mongodb collection to my meteorjs app

template

<head>
  <title>simple</title>
</head>

<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>
    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}

    </ul>
  </div>
</body>

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

main.js

tasks_list = new Mongo.Collection('todoList');
if(Meteor.isClient){
    Template.body.helpers({
     tasks: function(){
        return tasks_list.find();
     }
    });

}

if(Meteor.isServer){
    Meteor.startup(function(){
        // code to run on server at start up
    });
}

and I'm sure I have 'todoList' collection as I check it by 'db.todoList.find()' but sadly there are no records displaying from 'todoList' collection when I tried to render it unto my meteor app, any ideas, help please?

Your code seems ok. Make sure you either have the auto-publish packaged installed, or you manage the publish/subscribe correctly, so that when you run tasks_list.find() on the client, it actually finds the documents.

As a quick test, open the browser console, and type tasks_list.find().fecth() to see if the expected documents are returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM