Search code examples
mongodbmeteormongodb-querymeteor-helper

Where is the unexpected token here in this Meteor/MongoDB line of code?


I'm trying to filter the results of a MongoDB find() in my Meteor app in this way:

Template.tblScheduler.helpers({
  jobLocations: function() {

    // return JobLocations.find();
    return JobLocations.find({jl_jobloc}, {sort: {jl_jobloc: 1}});
  }
});

The commented out "cartesian result set" code works just fine, but I only need the one field in this case, and want them to be fetched in alphabetical order.

The app won't compile, though, complaining about line 40 of this line:

return JobLocations.find({jl_jobloc}, {sort: {jl_jobloc: 1}});

(which is the first "}" on that line).

What is wrong? Why is the "}" considered an "Unexpected token"?


Solution

  • The selector is incorrect in the find function

    According to the docs, http://docs.meteor.com/#/full/find

    The first argument is the selector

    {} - selects all documents

    The second argument is an object consisting:

    sort: {jl_jobloc: 1} - sorts the documents by jl_jobloc

    fields: {jl_jobloc: 1} - returns only the jl_jobloc field

    To put it all together to get what you need, try this:

    return JobLocations.find({}, {sort: {jl_jobloc: 1}, fields: {jl_jobloc: 1}});