themeteorchef / bert

A client side, multi-style alerts system for Meteor.
185 stars 27 forks source link

Exception while invoking Bert.alert #22

Closed raisudeen closed 8 years ago

raisudeen commented 8 years ago

Im very much impressed with your "Bert" package. But, unfortunately, Im getting below error while invoking "Bert.alert" method.

Exception while invoking method 'addOffer' TypeError: Cannot call method 'alert' of undefined

methods.js:

Meteor.methods({ addTask: function (rawData) { Bert.alert( 'Adding Offer', 'info', 'growl-top-right' ); } });

My build structure: todos |__ lib/methods.js

Hope above method is calling for both client and server, and Im getting exception while invoking in server. Can you please tell me how to fix it? Thanks alot in advance.

My Aim is to display a message in client once task added to its collection. i.e., I want to fire a message from server to show it in client that "Task added successfully".

Meteor version: 1.2.1 bert version: 2.1.0

themeteorchef commented 8 years ago

Howdy. This is because Bert is only accessible on the client side. If you want to invoke Bert from within a method in /lib, you need to wrap the call to bert in an if ( Meteor.isClient ) {} block. Something like this:

Meteor.methods({
  addTask: function (rawData) {
    if ( Meteor.isClient ) {
      Bert.alert( 'Adding Offer', 'info', 'growl-top-right' );
    }
  }
});

The reason is that the client side can see Bert.alert() defined as a function, while the server cannot. Does this help?

raisudeen commented 8 years ago

Thanks alot. Got it. :) Sorry for the late reply.

Amritpd commented 8 years ago

An alternative solution is wherever you are calling the server method from the client-side, use a callback function such as

Meteor.call("addOffer", this.username, function(err, offerAdded){
        if (offerAdded){
            Bert.alert( 'Offer has been added', 'success', 'growl-top-right' ); 
        }
    });

where addOffer may look like this:

Meteor.methods({
  addOffer: function (rawData) {
       Offers.insert(rawData);
       return true;
  }
});