Template.registerHelper issue, not able to access it using Template._globalHelpers statement


#1

Template.registerHelper(“timestampToTime”, function (timestamp) {
var date = new Date(timestamp);
var hours = date.getHours();
var minutes = “0” + date.getMinutes();
var seconds = “0” + date.getSeconds();
return hours + ‘:’ + minutes.substr(minutes.length-2) + ‘:’ + seconds.substr(seconds.length-2);
});

To access it - var result = Template._globalHelpers(“timestampToTime”)(timestamp);
No errors displayed but do I need to add anything or change the syntax ?
I even tried var result = Template._globalHelpers.timestampToTime(timestamp);


#2

Personally I have to use eg UI._globalHelpers.timestampToTime in order to access a global helper. You can just inspect JS objects through the (browser) console, that way you can tell what’s defined and where and how!

Also, it’s a good idea sometimes to not go through _globalHelpers, but instead to just define the helper as a regular global function first, and then call registerHelper using that function. That way you have it available both in Spacebars and in regular JS context.


#3

“click button.offer”: function(e,tpl,timestamp){
e.preventDefault();

    var description = tpl.$('input[name=offerInput]').val();

var result = UI._globalHelpers(“timestampToTime”)(timestamp);
// var result = Template._globalHelpers.timestampToTime(timestamp);

    var offerData = {
        ownerId: Meteor.userId(),
        name: Meteor.user().profile.name,
        description: tpl.$('input[name=offerInput]').val(),
        createdAt: new Date(),
        timestamp: Date.now(),
        serviceType:"offer",
        ownerImg: Meteor.user().profile.picture,
        phone: Meteor.user().profile.contact.phone,
        email: Meteor.user().profile.contact.email,

    };



    if(description){
        Services.insert(offerData, function(error, _id){
            if(error){
                alert(error);
            }
        });
    }
    else{
        window.alert("Please put in some sort of offer");
    }

    Session.set('submittedOffer', true);
},

  'click .give-me-more': function(evt) {
incrementLimit();

},

‘click .offerAgain’:function(e,tpl){
e.preventDefault();
Session.set(‘submittedOffer’, null);
Session.set(‘landingPageOffer’, null);
}

});

How can I easily use the following lines in my code to update time, date, hours etc… on 2 different pages
var date = new Date(timestamp);
var hours = date.getHours();
var minutes = “0” + date.getMinutes();
var seconds = “0” + date.getSeconds();


#4
timestampToTime = function (timestamp) {
var date = new Date(timestamp);
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
return hours + ':' + minutes.substr(minutes.length-2) + ':' + seconds.substr(seconds.length-2);
}
Template.registerHelper("timestampToTime", timestampToTime);

Put that in e.g. client/lib/timestamp.js and you will have it available across anywhere in your JavaScript and Spacebars.

The rest of your code is a bit of a mess, formatting-wise, so I did not look at that. Just answering your last question.


EDIT: And you still have/had a problem here:

var result = UI._globalHelpers("timestampToTime")(timestamp);
// should be either
var result = UI._globalHelpers.timestampToTime(timestamp);
// or
var result = UI._globalHelpers["timestampToTime"](timestamp);
// notice it's [], not () -- this is an array/map access, not a function call

#5

I have added the above piece of code to the timestamp.js file in lib under client… I want to access it across 2 different pages… What should I include or how can I access it ?
var result = UI._globalHelpers"timestampToTime";
This fixed the error but does not display the date & time functionality…
I am new to web development in general and I am working with my friend on a project, so I am learning web development through meteor… Most of my questions are silly as I get it, but please help me out… so that I can progress faster…
Thank you


#6

Use console.log(myVar) and the browser’s debugging tools in order to learn about what all the variables that you care about are, and whether your code is actually doing what you think it does.
Usually, when learning (web) development, the biggest issue is that what we write and thus tell the computer is not what we actually tell it to do. Being the machine that it is, it does what we really tell it to do, not what we think we would really like it to do. :smile:


#7

Hahaha, trueeee… I have created a button for posting and on clicking that button I want the posted message to display the time and date along with it…
This code is written by my friend
<<>>

{{#if submitted}}

something else

          {{else}}
          <form action="#" class="m-b-md ng-pristine ng-valid">
            <div class="input-group">
              <input type="text" class="form-control input-lg" placeholder="Fitness Coaching" name="offerInput" required="true" value ="{{defaultText}}">
              {{#if isLoggedIn}}

              <span class="input-group-btn">
               
                <button class="offer btn-lg btn-primary" type="button">Offer</button>
              </span>
           <!--   {{else}}
              <span class="input-group-btn">
                <button class="btn-lg btn-primary" type="button">Login First</button>
              </span>-->
              {{/if}}
            </div>

<<>>
Template.newOffer.events({

'click .user':function(){
    Session.set('currentId',this._id);
    var res=ChatRooms.findOne({_id: this._id});
    if(res)
    {
        //already room exists
        Session.set("roomid",res._id);

    }

    Session.set('isMessaging', true);
},

"click button.offer": function(e,tpl,timestamp){
    e.preventDefault();

    var description = tpl.$('input[name=offerInput]').val();

var result = UI._globalHelpers"timestampToTime";
//var result = Template._globalHelpers(“timestampToTime”)(timestamp);
// var result = Template._globalHelpers.timestampToTime(timestamp);

    var offerData = {
        ownerId: Meteor.userId(),
        name: Meteor.user().profile.name,
        description: tpl.$('input[name=offerInput]').val(),
        createdAt: new Date(),
        timestamp: Date.now(),
        serviceType:"offer",
        ownerImg: Meteor.user().profile.picture,
        phone: Meteor.user().profile.contact.phone,
        email: Meteor.user().profile.contact.email,

    };



    if(description){
        Services.insert(offerData, function(error, _id){
            if(error){
                alert(error);
            }
        });
    }
    else{
        window.alert("Please put in some sort of offer");
    }

    Session.set('submittedOffer', true);
},

  'click .give-me-more': function(evt) {
incrementLimit();

},

‘click .offerAgain’:function(e,tpl){
e.preventDefault();
Session.set(‘submittedOffer’, null);
Session.set(‘landingPageOffer’, null);
}

});

How can I fix my problem ?


#8

Well, you should put something in the html template that will display that message, i.e. define a helper. And that helper could operate on that message value that you set from within the event handler as Session.set('myDateString') or similar.


#9

“click button.offer”: function(e,tpl,timestamp){
e.preventDefault();

    var description = tpl.$('input[name=offerInput]').val();

var result = UI._globalHelpers"timestampToTime";
//var result = Template._globalHelpers(“timestampToTime”)(timestamp);
// var result = Template._globalHelpers.timestampToTime(timestamp);

    var offerData = {
        ownerId: Meteor.userId(),
        name: Meteor.user().profile.name,
        description: tpl.$('input[name=offerInput]').val(),
        createdAt: new Date(),
        timestamp: Date.now(),
        serviceType:"offer",
        ownerImg: Meteor.user().profile.picture,
        phone: Meteor.user().profile.contact.phone,
        email: Meteor.user().profile.contact.email,

    };

Session.set(‘display date’, true);

    if(description){
        Services.insert(offerData, function(error, _id){
            if(error){
                alert(error);
            }
        });
    }
    else{
        window.alert("Please put in some sort of offer");
    }

    Session.set('submittedOffer', true);
},

  'click .give-me-more': function(evt) {
incrementLimit();

},

‘click .offerAgain’:function(e,tpl){
e.preventDefault();
Session.set(‘submittedOffer’, null);
Session.set(‘landingPageOffer’, null);
}

});

Template.newOffer.helpers({
userInteraction: function(){
return ChatRooms.find({chatIds: Meteor.userId()});
},
contactable:function(){
return Meteor.user().profile.contact.email;
},
offer: function() {
return Services.find({serviceType: ‘offer’}, {sort: {createdAt: -1}, limit: Session.get(‘limitOffer’)});
},

isMoreOffers: function(){
return (Services.find({serviceType: ‘offer’}, {sort: {createdAt: -1}, limit: 1+Session.get(‘limitOffer’)}).count()>Session.get(‘limitOffer’));
},

submitted:function(){
return Session.get(‘submittedOffer’);
},

defaultText: function(){
return Session.get(‘landingPageOffer’);
},

isLoggedIn:function(){
return Meteor.user();
},

timestampToTime:function(){
return Session.get(‘display date’);
}

});

I have added
Session.set(‘display date’, true);
and
timestampToTime:function(){
return Session.get(‘display date’);
}

to helper… Is this correct ?
what am I supposed to add on html…?


#10

You really have to work through the Meteor introduction if you want to understand: https://www.meteor.com/tutorials/blaze/creating-an-app
Specifically it gives an example of what I meant in this chapter: https://www.meteor.com/tutorials/blaze/temporary-ui-state


#11

Thanks for the links… I will work on it


#12

The simple todo list app was very useful. Thank you. I have the data stored in the database with date,time and other associated data with each post. I only need the date and time from those posts in database to display along with the post on the front end. How can I access the database(all details stored on server side) to get this part of data and keeping the remaining data as it is ?
I have removed the insecure and autopublish packages


#13

Have you read this part about pub/sub? – https://www.meteor.com/tutorials/blaze/publish-and-subscribe

You may want to clone the example code from the tutorial to your local machine and fire up the meteor instance and play with the code. Add the msavin:mongol package to give you easy insight into what’s currently loaded on the client from the server-side DB, so you can play with and test out publications & subscriptions!


#14

Hello,
I am trying to set-up sending verification emails to users who create an account.
From various docs and posts in stackoverflow, I used the following code on server side
Accounts.config({
sendVerificationEmail: true,
forbidClientAccountCreation: false
});
Since it was mentioned not to include it in the Accounts.onCreateuser I didn’t add it to the function.
I have added the relevant packages accounts-ui, accounts-password, accounts-base, accounts-unstyled, email packages.

And tried to incorporate changes but the feature is not working and I am not getting any verification email. Do I need to do any changes w.r.t smtp server ?
Can you help me a bit ?


#15

You have to also set the MAIL_URL environment variable, or set process.env.MAIL_URL in your JavaScript on the server.

Relevant section of the Meteor docs: http://docs.meteor.com/#/full/email

The server reads from the MAIL_URL environment variable to determine how to send mail. Currently, Meteor supports sending mail over SMTP; the MAIL_URL environment variable should be of the form smtp://USERNAME:PASSWORD@HOST:PORT/. For apps deployed with meteor deploy, MAIL_URL defaults to an account (provided by Mailgun) which allows apps to send up to 200 emails per day; you may override this default by assigning to process.env.MAIL_URL before your first call to Email.send.

You can either use a local or remote smtp if you have easy access to one, or otherwise it’s recommended to sign up with Mailgun and get a free smtp account from them. Use their sandbox account if you want or go all the way and configure your domain for verified sending through Mailgun (recommended for production apps).


#16

Hello,
Can meteor files be exported or converted into another file format and used on another platforms ?
We have developed a webapp and our customer wants it to be run or accessible using prototyping apps like Justinmind, invision etc.
Does meteor offer a way to export the complete webapp developed using meteor onto a different platform ?


#17

http://themeteorchef.com/recipes/exporting-data-from-your-meteor-application/http://themeteorchef.com/recipes/exporting-data-from-your-meteor-application/

Are there any articles of this type you know that can solve my problem(for html, css, js)