Server's variable can't retrieve from Client


#1
// Server side 

Meteor.startup (function (){
	var db = new MongoInternals.RemoteCollectionDriver('mongodb://127.0.0.1:3001/RecSys');
	var Films = db.open('Films');
	var Users = db.open('Users');
	var Rates = db.open('Rates');


	console.log(Films.find().count()); // show correct result
	console.log(Users.find().count()); // show correct result
	console.log(Rates.find().count()); // show correct result
});

// But in the Client side, I can't access to var Films, Users, Rates
// Console error: Can't find variable Films
Template.films.helpers({films : 
      Films.find({},{sort:{rated:-1}})
	});

#2

You need to create the Films, Users and Rates collections on the client too… they’re not created automatically


#3

you mean : in Client I have to do:

var Films = new Mongo.Collection('Films'); ???

#4

to make this works you will need:

  1. setup your collections on the client and server too (easiest to put them in lib/collections.js) like
    Films = new Mongo.Collection('films');

  2. you need to setup publications and subscriptions http://guide.meteor.com/data-loading.html
    (or just meteor add autopublish (not recomended for production))

hope this helps


#5

Yes - can I ask why you are using MongoInternals instead of Mongo.Collection?


#6

Yes, because I had put my data in mongodb , so in server side, I have to connect to mongo server for retrieving my data, take it and show in html page. And I think I don’t need to re-create new Collection in Client side while data already existing in server side and it doesn’t make sense for me.


#7

When you create a collection on the client, it uses the MiniMongo API to create an in-memory store (it doesn’t use MongoDB) which is empty. Then you use the Publish/Subscribe to feed data from the server database into the client store. It’s like creating a plain array/object.