Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameters in Meteor router

I have a path in Meteor

path: '/my-url/:groupId?',

I have added the question mark to indicate that sometimes groupId is not used. In data I check whether the group id is set with this.params.hasOwnProperty('groupId') but I have come across that sometimes the router thinks groupId is set even if it isn't (don't know why) but the value is undefined.

Therefore, I tried

console.log(this.params.hasOwnProperty('groupId'));
console.log('groupId' in this.params);
console.log(this.params.groupId);

and they evaluate to

> true
> true
> undefined

So I guess hasOwnProperty is not the best way to check whether groupId is set since it doesn't check for undefined values.

What will be a better way to check this? Why does hasOwnProperty evaluate to true even if my url is /my-url?

like image 250
Jamgreen Avatar asked Mar 13 '26 20:03

Jamgreen


1 Answers

I think you've already answered yourself on your question. I have same problem and my check is:

if (this.params.groupId) {
} else {
}

This is not yet confirmed, but I think as long as you provide :groupId, you're already have that in params, regardless it exists or not.

like image 153
jwall Avatar answered Mar 16 '26 09:03

jwall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!