0

I have server & client files and am trying to send some data to Javascript rather than into a template. In the template, I can output some values, but I need it in JS to add markers to Leaflet.

I guess, there's no sense to funnel the data through templates to get it into JS and into Leaflet, right?

What am I doing wrong?

shell

$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use atms
switched to db atms
> db.markers.count()
1868

Running the server:

$ MONGO_URL=mongodb://127.0.0.1:27017/atms meteor

lib/collections.js

Markers = new Meteor.Collection('markers');

In the client/client.js, I try to get records from the collection, but it's empty:

Template.hello.helpers({
    marks: function () {
        // this data renders correctly on map
        return Markers.findOne({})
    }
});

Template.hello.onRendered(function() {
    // this data is empty in console
    var query = Markers.find().fetch();
    console.log(query);
});

In the template, it shows one record, which means the connection works. But console output is [].

culebrón
  • 34,265
  • 20
  • 72
  • 110

2 Answers2

1

The main problem is you have 2 different Markers collection on client and server. So, on the client, meteor accesses client Marker collections and does not show any data. Collection definition should be shared between client and server, not duplicated.

Make a folder lib and put the collection definitions there

//lib/collections.js
Markers = new Mongo.Collection('markers')

And remove the collection definitions in both server and client folder.

Also, be aware that when you use the separated mongo instance, the reactivity will happen quite slow (2 or 3 times compared to the embedded mongo)

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • The code kind of worked, without `var` keyword (otherwise Markers is again undefined). I restarted with `MONGO_URL`, no crashes, but still an empty collection. – culebrón Feb 24 '16 at 21:46
  • sorry bout the `var`. i copied/pasted your code and didnt see that. – Thai Tran Feb 24 '16 at 21:49
  • I managed to push one record through the templates, but why `Markers.find().fetch()` does not show anything in console.log? – culebrón Feb 24 '16 at 21:53
  • it takes times to return the data because you are using the separated mongo. Sorry, i just out for the meeting and just go back and see you mark answer as done. Thanks mate lol – Thai Tran Feb 24 '16 at 22:17
0

Pretty sure this is answered here:

Using Multiple Mongodb Databases with Meteor.js

Just a note on the syntax, in ver 1.2.1 you want to declare your Meteor collection as a global variable so it can be accessed outside of the file you type it into. Also, you want to put this line in /lib or a directory that can be accessed by both the client and server.

Markers = new Mongo.Collection('markers')
Community
  • 1
  • 1
bobNelson
  • 38
  • 6
  • I don't get it. In `server.js` I run that connection with `_driver` and it seems to be ok. But in the `client.js` I leave it the same, and nothing changes, still an empty array. If I remove `Marker` declaration from `client`, then it's undefined and js crashes. But I can't copy that `_driver` code, because `MongoInternals` isn't defined in cilent. – culebrón Feb 24 '16 at 21:37