I am doing a Meteor.call('searchDatabase', keys...)
that is executed whenever a user submits a search. I am currently passing an array of the words submitted called keys
. However, I do not know how to do the necessary check(keys, ?)
on the server side. I originally thought that I could do keys.forEach(function(element) { check(element, String)}
, but i still get a Did not check() all arguments
error. Should i just pass the submitted search as its original string in the Meteor method call and then break it up on the server? or is there a way to check that keys is an array?
I am doing a Meteor.call('searchDatabase', keys...)
that is executed whenever a user submits a search. I am currently passing an array of the words submitted called keys
. However, I do not know how to do the necessary check(keys, ?)
on the server side. I originally thought that I could do keys.forEach(function(element) { check(element, String)}
, but i still get a Did not check() all arguments
error. Should i just pass the submitted search as its original string in the Meteor method call and then break it up on the server? or is there a way to check that keys is an array?
4 Answers
Reset to default 16If keys
is an array of strings, you can just do:
check(keys, [String]);
Your method would look something like:
Meteor.methods({
searchDatabase: function(keys) {
check(keys, [String]);
// add other method code here
}
})
In case it helps anybody else, I recast the answer from the Meteor forums to use arrow functions and avoid underscore and duplicate declarations:
check(subscriptions, Match.Where((myArray) => {
myArray.forEach((myObject) => {
/* do your checks and return false if there is a problem */
});
// return true if there is no problem
return true;
}));
This checks an array of objects.
As shown here: https://forums.meteor./t/check-object-in-an-array/3355
var subscriptions = [
{/* ... */},
{/* ... */},
{/* ... */}
];
check(subscriptions, Match.Where(function(subscriptions){
_.each(subscriptions, function (doc) {
/* do your checks and return false if there is a problem */
});
// return true if there is no problem
return true;
}));
If you use simple-schema, you should try this way:
check( keys, [ mySchema ] );
You can learn more about check patterns in this link using-the-check-package