Meteor - Check


Advertisements


The check method is used for find out if the argument or types are matching the pattern.

Installing Check Package

Open the command prompt window and install the package.

C:\Users\username\Desktop\meteorApp>meteor add check

Using Check

In the following example, we want to check if myValue is a string. Since it is true, the app will proceed without any errors.

meteorApp.js

var myValue = 'My Value...';
check(myValue, String);

In this example, myValue is not a string but a number, hence the console will log an error.

meteorApp.js

var myValue = 1;
check(myValue, String);
Meteor Check Log Error

Match Test

The Match.test is similar to check, the difference being when the test fails instead of a console error, we will get a value without breaking the server. The following example shows how to test an object with multiple keys.

meteorApp.js

var myObject = {
   key1 : "Value 1...",
   key2 : "Value 2..."
}

var myTest = Match.test(myObject, {
   key1: String,
   key2: String
});

if ( myTest ) {
   console.log("Test is TRUE...");
} else {
   console.log("Test is FALSE...");
}

Since the both keys are strings, the test is true. The console will log the first option.

Meteor Match Test True

If we change the key2 to number, the test will fail and the console will log the second option.

meteorApp.js

var myObject = {
   key1 : "Value 1...",
   key2 : 1
}

var myValue = 1;

var myTest = Match.test(myObject, {
   key1: String,
   key2: String
});

if ( myTest ) {
   console.log("Test is TRUE...");
} else {
   console.log("Test is FALSE...");
}
Meteor Match Test False

Advertisements
E-Books Store