Connecting mongodb with meteor app

Hi there, i am learning how to build apps with meteor and mongodb from http://www.chadmorita.com/meteor-example-fullcalendar/. After i ran npm install from the direction and then ran my apps, i checked in robomongo and it worked. It created the EventData collection. I created a new page named login, and i want to create a new collection to the initial localhost:3001/meteor.

I wrote a few lines of code like these in meteor client (my apps running in client) :

import { Mongo } from ‘meteor/mongo’;

const eventLogin = new Mongo.Collection(‘Login’);
Meteor.subscribe(‘eventLogin’);

const counts = eventLogin.find({},{user_id : userVar, pass: passVar}).count();
console.log(counts);

i always get counts=0. What should i do to solve this problem?

Thank you very much for helping me.

Most probably it’s this line:

const counts = eventLogin.find({},{user_id : userVar, pass: passVar}).count();

Meteor’s mongo api is slightly different from the one in mongo shell. Try:

const counts = eventLogin.find({user_id : userVar, pass: passVar}).count();
1 Like

Thank you @gaurav7, i tried many times, but it didn’t work.

Here is the screenshot.

Please advice.

You have to wait until the subscription is ready.

const sub = Meteor.subscribe('eventLogin');
if (sub.ready()) {
  // now you have data in minimongo, then you can work with your data on client side
}
2 Likes

Hi @minhna, thank you for answering, i have tried the code, but it didn’t return anything.
The code exactly like these:

const sub = Meteor.subscribe(‘eventLogin’);
if (sub.ready()) {
Console.log(“x”);
}

You need to put it in a tracker or something.

Tracker.autorun(() => {
  const sub = Meteor.subscribe('eventLogin');
  if (sub.ready()) {
    // now you have data in minimongo, then you can work with your data on client side
  }
});
1 Like

I am checking a logs in robomongo. At the 3rd line before the end, i find statement “connecting to localhost:27017”, does meteor have another port to connect or as default use 3001?

image

if you run meteor on development and you run meteor with the default port 3000, the mongodb will run on port meteor port + 1 = 3000 + 1 = 3001.
the port 27017 is the default port of Mongodb server if you install it as a service.

@minhna, anyway i try to include eventlogin.js in eventdata.js (api\eventdata\eventdata.js) and it works, but when i separate eventlogin.js and i call new mongo.collection in login.js, eventLogin.find returns 0

check this file: https://github.com/chadmorita/meteor-example-fullcalendar/blob/master/app/imports/startup/server/publications.js
you will need to import the eventlogin.js file you created here.

I have it already.

and here is the result

It told you that you need to install bcrypt lib.
just run meteor npm install --save bcrypt

i have done it @minhna, still the same result, so i give a comment to this line.

import {eventlogin} from ‘…/…/api/eventlogin/eventlogin.js’

this is my eventlogin.js

I can’t see the rest of the file but you need to export the eventlogin to be able to import it.
export const eventlogin

is it the same with export const eventlogin?

Can I ask why you are creating your own login collection?

Is there a reason you aren’t using the Meteor Accounts packages?

No, it’s not.
If you do export default eventlogin, then when you import it
import eventlogin from '..path to eventlogin file';

Hi @coagmano, i just try connecting mongodb with meteor and login form was the first form i have thinked about. I think if i can connect both of them, i can create another form at next development.

I am newbie in it. Would you teach me how to use meteor accounts packages? thanks.

As for the original "counts always equal "0 problem @minhna is right about the subscription not being ready.

Ideally you should always subscribe to the data needed in the onCreated callback of your template:

Template.App_Login.onCreated(function() {
  // use this.subscribe instead of Meteor.subscribe so Blaze will automatically 
  // clean up the subscription when the template is un-rendered
  this.subscribe('eventlogin');
});

If you do this, you’ll notice that after some time (usually <50ms in development) the counts will be accurate.

You can wait for the subscription to be ready with:

  • a callback to subscribe:
    this.subscribe('eventlogin', function () {
      const counts = eventLogin.find({user_id : userVar, pass: passVar}).count();
      console.log(counts);
    });
    
  • An autorun (These are functions that re-run when a reactive source, like find, change)
    this.subscribe('eventlogin');
    // use this.autorun instead of Tracker.autorun so Blaze will automatically 
    // clean up the autorun when the template is un-rendered
    this.autorun(function() {
      // Check if the subscription is ready first. This is reactive and will cause a re-run
      if (!Template.instance().subscriptionsReady()) return;
      
      const counts = eventLogin.find({user_id : userVar, pass: passVar}).count();
      console.log(counts);
    });
    
  • Or a helper! Helpers are automatically reactive in Blaze, so even if there’s no data when it first runs, it will update when the data comes through the subscription
    Template.App_Login.helpers({
      count: function () {
        return eventLogin.find({user_id : userVar, pass: passVar}).count();
      }
    });
    

I’ve also noticed in a lot of your examples, the capitalisation of variables and strings aren’t always consistent. JS is case-sensitive, so this might also be contributing to the issues you’re experiencing

2 Likes