Olivier Girardot
Olivier Girardot

Reputation: 409

Meteor.js and MongoDB, I have problems manipulating my Databases

I am absolutely new to both MongoDB and Meteor.js. I am trying to follow a tutorial, and despite the fact that I am reproducing exactly what the teacher does, I am stuck. I am trying to display the list of items from the database entries but nothing shows up. I tried many ways, and console.logged quite a lot but I never managed to get it working.

Here are my files:

1/ main.html

<body>
  <h3 class="title">Welcome!</h3>

  <div class="container list">
    <ul class="collection">
      {{#each entries}}
        {{> list}}
      {{/each}}
    </ul>
  </div>

</body>

<template name="list">
  <li class="collection-item"> {{title}}
  </li>
</template>

2/ main.js

import { event } from 'jquery';
import { Template } from 'meteor/templating';
import { Entries } from '../lib/collection.js';

import './main.html';

Template.body.helpers({
  entries(){
    return [Entries.find({})];
  },
});

3/ collection.js

import { Mongo } from 'meteor/mongo';

export const Entries = new Mongo.Collection('entries');

And here is the database that I have created in the command line after running meteor mongo, and when I type in the command: db.entries.find().pretty()

{
        "_id" : ObjectId("5f69ef84eda325daca4b0d51"),
        "title" : "Hello",
        "content" : "try hard"
}
{
        "_id" : ObjectId("5f69f0db434cda7b7f21d193"),
        "title" : "Hello",
        "createdAt" : ISODate("2020-09-22T12:40:59.924Z")
}
{
        "_id" : ObjectId("5f69f1c8434cda7b7f21d194"),
        "title" : "Hello",
        "createdAt" : ISODate("2020-09-22T12:44:56.230Z")
}
{ "_id" : ObjectId("5f69f2c3434cda7b7f21d195"), "name" : "Watercress" }
{
        "_id" : ObjectId("5f69f6012a6382ef314d84cb"),
        "title" : "goodbye",
        "createdAt" : ISODate("2020-09-22T13:02:57.511Z")
}
{
        "_id" : ObjectId("5f69f6bf2a6382ef314d84cd"),
        "title" : "fresh",
        "createdAt" : ISODate("2020-09-22T13:06:07.561Z")
}

If you could help, that would be great!

Upvotes: 0

Views: 96

Answers (1)

SimonSimCity
SimonSimCity

Reputation: 6572

I know, it's quite frustrating to start off on a tutorial, facing that it's not working as expected, and I hope the following tips help you diving into how Meteor and its magic work.

  1. Ensure the collection is registered both on the server and the client. For you this means, ensure the file collection.js runs on both environments. You can confirm this by putting a console.log() into the file. If you see the output on both the terminal and in the console of the browser, continue. This is specially important when using autopublish package.
  2. Ensure the data is published to the client. This happens by either
    2.1 installing the package autopublish (have a look at the file .meteor/packages. In case you don't have it, you can install it using meteor add autopublish) or
    2.2 jump right away to manually publishing the data (https://www.meteor.com/tutorials/blaze/publish-and-subscribe).

    Install the extension Meteor DevTools for Chrome, open the developer toolbar, switch to the tab Meteor and there to the inner tab Minimongo to confirm the data has arrived at the browser.
  3. Now it's the job of Blaze to get informed about the changes (that data is coming into the collection on the browser) and updating it.

Here's a very good article about Meteors publications & subscriptions. It's old but gold for understanding the concept.

If you have questions where you have the impression it needs a back-and-forth communication, please join the the Meteor community on Slack.

Upvotes: 1

Related Questions