If true false conditions in Templates

Inside the template, I have

{{#if validation}}
   <span> First </span>
{{else}}
   <span> Second </span>
{{/if}}

where validation is the boolean value false
Why is it showing “First” instead of “Second” even though the value of validation is false?

Thanks.

It shouldn’t display first, something seems to be wrong with your helper return value. Can you try to display it just to check that it is indeed set to false?

1 Like

@MazDev Thanks.
You are right. I guess the problem is with the helper itself.
Its not updating reactively. Any suggestions.

What’s the helper function look like? Hard to diagnose something no one can see :slight_smile:

2 Likes

Hi @nkrisc Thanks. The helper looks like this:

Template.relatedBlogsTemplate.helpers({
     validation: function() {
            var setting = BlogSettings.findOne({});
            if (setting) {
                return setting['validationStatus'];
            }
}

First I am getting the BlogSettings document. It will return the document related to the userId. So, only one document.

I am saving that document in setting. Then I am getting the validationStatus which is a boolean value.

I recommend to have publications and subscriptions to handle it. Have a look at this repo to see how to achieve it in a reactive way.

You need to use Tracker which is included in core.

Your conditional is not reactive, so you need to wrap it with tracker to make it react to the BlogSettings changes.

Template.relatedBlogsTemplate.helpers({
     validation: function() {
            var setting = BlogSettings.findOne({});
            Tracker.autorun(function () {
                if (setting) {
                    return setting['validationStatus'];
                }
           });
}

I haven’t tested it but it should run ok.

No, now will the validation helper never return a value. And you never use a reactive variable in your autorun, so it will only run once. And helpers are reactive, so there’s no need to use Tracker.autorun at all.

It seems you’re right, my bad.

Do you think something like this would do the trick then?

Template.relatedBlogsTemplate.helpers({
     validation: function() {
          return BlogSettings.findOne({});
}

BlogSettings.findOne({}) is a reactive data source, so your helper should rerun everytime when any document is added/changed/removed with a collection. It should works. Are you shure you have any documents in a BlogSettings collection? Or founded document has a validationStatus field?

Yes I have the documents. When I directly use

return BlogSettings.findOne({})['validationStatus'];

and when I open developer console, I see error message: cannot call validationStatus of undefined first but the template works.

Actually, the helper is being called multiple times even though there is only one template which is calling it.

This is the way I would do it, you can use Fields to read the changes just what you need. if still fails sure the error is not in the helper

Template.relatedBlogsTemplate.helpers({
	validation: function () {
		var setting = BlogSettings.findOne({}, {fields: {validationStatus: 1}});
		return setting && setting.validationStatus;
	}
});
1 Like

Thats right! Blaze call your helper every time when any of reactive source is triggered. I gues that when your template is rendering for the first time there is no documents in a collection.
This one could be broken

return BlogSettings.findOne({})['validationStatus'];

So your first version of helper should works by default. You could set a breakpoint in the helper function and dig the data from a collection.

1 Like

@mrzafod @cottz

Thanks. The helper is reactive now and it is working. The problem is with the if else condition inside the template.

I dont understand why but the false condition is working and not true condition.

Ok, I figured out the problem. The format of true or false in database is in String format and not in Boolean. This caused the problem till now.