Server side collection field to value inside iron router

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

i get a nice value like this

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 { 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!

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


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.

1 Like

@robfallows was quicker :wink:

1 Like

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 {

i get this crap:
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')

1 Like