1

I need to check for a value of an object field and depending on that I want to set an ReactiveVar variable. But this field is just an optional one, so maybe it doesn't exists at all. What is the correct way to check the value?

If I would just use var result = (data.profile.anyField == 'something' ) I would get an error as I can't use anyField of undefined, if profile isn't set.

So I would do it like this, but I think it can be done a little bit smarter and shorter:

var modus;
if (Data.profile) modus = (Data.profile.anyField == 'something') ? 'setValue' : '';
else modus = '';

this.modus = new ReactiveVar(modus);

And the second question is if it is correct to set the value of modus to an empty string? The result should be like this:

this.modus = new ReactiveVar('setValue'); // if modus is true
this.modus = new ReactiveVar();           // if modus is false
1
  • What does the ReativeVar do? It depends on that if you can or can't pass in an empty string. You could do var modus = data && data.profile && data.profile.anyField == 'something' ? 'setValue' : ''; Commented May 5, 2016 at 9:24

1 Answer 1

1

Question 1:

A one-liner:

new ReactiveVar(Data.profile && Data.profile.anyField == 'something' ? 'setValue' : '');

Question 2:

Replace the empty string with undefined

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.