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’);
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
}
});
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?
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
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:
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
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