Client Side increment of a number, or javascript math / access to collection data

I have a little project I’m playing with, that starts at slide 1. When I save, I want to show the user the last slide number (no problem, can pull that with a simple query in a helper) + 1. The + 1 is where I’m stuck.

I can’t seem to figure out how to access the return data in my javascript, and can’t do math in the template spacebar.

I know there must be a way to do this. it’s really display only until I’m ready to save the ‘next’ slide.

So, for collection slides I have

{
    slideNumber: 1,
    owner: Meteor.users().emails[0].address,
    slideBody: <some html as a string>,
    createdOn: new Date(),
}

I get back slideNumber, and can display it in the UI, but I want to display the next incremental number for the user so they know they are working on slide 2, not 1.

I’m sure this is simple, and I’m just missing it somehow.

my query is

Slides.find({}), {sort: { _id: -1 }, { limit: 1 });

which gives me the last slide created, but when I try to use it in a helper like this:

Template.getSlides.helpers({
    var lastSlide = Slides.find({}), {sort: {  _id: -1 }, { limit: 1 });
    var slideNo = lastSlide.slideNumber;
    var nextSlideNo = slideNo + 1;
});

I get an undefined message in the console.

Any help is greatly appreciated.

Meteor 1.4.2.3 I believe (in case that matters).

  • you should not sort by _id, because this is a random-string.
  • You should sort by slideNumber
  • collection.find returns a cursor. Use findOne to get one document:

const lastSlide = Slides.findOne({}), {sort: { slideNumber: -1 }, { limit: 1 });

Thanks @macrozone I’ll give it a shot.