Server side collection field to value inside iron router


#1

In the MongoDB, if I use this
db.logs.find({}, {_id: 0, text: 1})[0].text

i get a nice value like this
14,0,google,TS,4,14,,,,,14,,,,

but when I use this in my file /imports/api/urls.js
logs.find({}, {_id: 0, text: 1})[0].text

it returns this
Cannot read property 'text' of undefined

The reason for doing it in the /imports/api/urls.js is because i’m using Router.map(function() { and for some reason it only works in that specific file.

I’m struggling to get this piece of struggles ready for my exams on Monday, so it doesn’t need to be a clean solution, as long as it works!

Any help is much much much appreciated!


#2
logs.findOne({}, {_id: 0, text: 1}).text

or

logs.find({}, {_id: 0, text: 1}).fetch()[0].text

find returns a cursor, but you are wanting an object (the first object). findOne will return the first object. find().fetch() will return an array of objects, so [0] will be the first.


#3

@robfallows was quicker :wink:


#4

Saved my ass, thanks @robfallows!!

oke, sooooo, little bit of a problem which I don’t fully understand:

using both of your solutions which work great in my small test space, apparently doesn’t work when used inside this

Router.map(function() {

i get this crap:
iron:router
Organize your Meteor application.

Router.route('/', function () {
  this.render('Home', {
    data: function () { return Items.findOne({_id: this.params._id}); }
  });
});

with this error:
undefined is not an object (evaluating 'logs.findOne({}, { _id: 0, text: 1 }).text')