socialight
socialight

Reputation: 517

.deny Meteor use.. can't get it to work

I'm trying to deny a question to be submitted if it has a length of 0. But I don't quite understand Meteor deny.

Here's what's going on.

I am updating the question. It is currently set at "yes"

I update it to "yessir"

I console log it as follows:

Questions.deny({
  update: function(userId, question) {
    console.log(question.question.length);
   }
});

but the result is 3. It seems to console log the field being updated, not what I am updating it TO.

This is a problem because how can I check the length of an input if this thing won't check it when it's being submitted.

Can someone enlighten me?

Upvotes: 0

Views: 47

Answers (2)

Shankar Regmi
Shankar Regmi

Reputation: 874

Collection.deny Function either returns true or flase. If you want to deny update on certain criteria here goes your code like this

Questions.deny({
update: function(userId, question, fields, modifier) {
// check for critera
if(fields.question.length < 0)
return true // denys update for question length less than 0
else
return false // deny = false means allow = true

}
});

Upvotes: 0

David Weldon
David Weldon

Reputation: 64342

Have a look at the docs and you'll see that the 2nd argument to update is doc:

doc is the current version of the document from the database, without the proposed update

The only way to validate the length of question is to look at the 4th argument - modifier. The problem with this approach is that you must check the modifier for every possible way it could be modified. Fundamentally, this is why allow/deny is really hard to implement in all but the most simple cases.

Instead, I'd strongly suggest either using collection2 to enforce your schema or using methods to modify your documents.

Recommended reading:

Upvotes: 2

Related Questions