Meteor Methods ignore return when return data is 0 (zero)


#1

I have a Methods that check collection count so my Methods return is as follow:

let collectionName = CollectionName.find();
return collectionName.count()

If the count result is not == zero, the return work as expected. But if the count return zero, the Methods doesn’t return anything, client side Methods callback function doesn’t trigger at all.

Why such behavior and what is the good of it?

Please advice, thank you.


#2

Anyone can help answer this?


#3

Can we see your method code and how you call it?


#4

Hi,

Thanks and the following is the Method and how I call it.

Meteor.methods({
    outletCount: function () {
        let outlets = Outlets.find();
        return outlets.count()
    }
});
Template.templateName.onCreated(function () {
Meteor.call('outletCount', function (error, success) {
    if (error) {
      console.log('error', error);
    }
    if (success) {
      console.log('success', success);
    }
  });
});

If outlet count is greater than zero, then console.log success is trigger. But if outlet count is zero, both console.log on error and success also not trigger.

Please advice why such behavior and what is the good of it if Meteor design it this way cause I guess it’s not a bug.

Thanks in advance.


#5

Hi,

I think the method callback is actually called but you are checking for a falsy success value and the condition wont be true for 0.

Use this or something similar:

Template.templateName.onCreated(function () {
  Meteor.call('outletCount', function (error, success) {
    if (error) {
      console.log('error', error);
    } else {
      console.log('success', success);
    }
  });
});

Hope this helps.


#6

@pmogollon

Ah Yes! You’re right. Stupid me for checking falsy with IF. I got the respond now. Thank you very much.

Have a good day.