简体   繁体   中英

Get data from Meteor Collection for CountdownTimer

I want to set a CountdownTimer to a specific date what the user has picked. I saved the date in a Meteor Collection

Template.addDate.events({
    'submit form':function(event){
         var closeDate = $('[name="date"]').val();
     Date.insert({
    closeDateDB: closeDate,
         )};
    }
});

Now i want to get this Data from the Collection and set my new endtime in my Timer to this picked Date.

Meteor.startup(function () {
    var endtime = Data.findOne(???);
    timeinterval = setInterval(function () {
      Meteor.call("getCurrentTime", function (error, result) {
        Session.set("time", result);
        var t = getTimeRemaining(endtime);
        Session.set("t", t);
      });
    }, 1000);
  });

How can i get the inserted date in my Database and use it for my Timer? How to fill the "???" in

Data.findOne(???);

EDIT___

I try to define better what i want to achieve. If the user enters the site he shoud fill out a form. Something like this:

Template.addQuestions.events({
    'submit form':function(event){
      event.preventDefault();
      var questionText = $('[name="questionName"]').val();
      var categoryText = $('[name="categoryName"]').val();
      var closeDate = $('[name="date"]').val();
      Questions.insert({
        closeDateDB: closeDate,
        categoryDB1: categoryText,
        questionDB: questionText,
        createdAt: new Date(),
      }, function (error,results){
      Router.go('decision', {_id:results});
      }

As you can see i got the closeDateDB where the user is able to pick a date by a datepicker. I used tsega:bootstrap3-datetimepicker . After this the user is redirected to a new created site.

On this new site i wanna get the closeDate for example "12/24/2015" for my countdowntimer as endtime in the Meteor.startup(); The counter should count from the currentTime, the actual date/time when the form is submitted, to the closeDate date/time the user entered before in the form. When the actual date/time > closeDate , the created site should be closed and the user gets redirected to another page.

I'm reading this as a modeling problem. When you have:

 Date.insert({ closeDateDB: closeDate });

You are creating a record in MongoDB with a single key closeDateDB .

Firstly this isn't indexed by user so it's not clear what you plan to do with these events. Meteor.startup() will run for every new user (on either the client or server side, depending on where you define it). Does each one get his/her own action? What happens when there are multiple events in the future?

Secondly, what happens when time is > closeDate and the event is past ?

Some possibilities:

// find a single random event in the future
Date.findOne({ closeDateDB: { $gte: new Date() } }); 

// find the next event
Date.findOne({ closeDateDB: { $gte: new Date() } },
  { sort: { closeDateDB: -1}} );

// find the next event for this user (assuming the userId key was added on insert)
Date.findOne({ userId: Meteor.userId(), closeDateDB: { $gte: new Date() } },
  { sort: { closeDateDB: -1}, limit: 1 } ); )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM