Server-side data validation in Meteor

I have a form and a submit function in a client file:

function submitme () { var message = $('#daform').serializeJSON(); message.owner = Meteor.user().username; if(!message.description || !message.location.lat || !message.location.lng || !message.mysex || !message.yoursex) { return; } else { lists.insert(message); console.log("Submitted!"); $('#daform')[0].reset(); } } 

This works very well, although it is the CLIENT validation side => not protected.

How do I check for backup checks in my server file? (+ bonus question: how to set a timer so that after sending you had to wait X seconds before resending?)

+6
source share
1 answer

You can use http://docs.meteor.com/#deny (you can use allow, but it may be easier to place the verification material in a separate rejection), since negation will be override if it should not be inserted:

It works the same as the backup method on the server before inserting it.

With your message collection

Js server

 message.deny({ insert: function (userId, doc) { return (!doc.description || !doc.location.lat || !doc.location.lng || !doc.mysex || !doc.yoursex); }, update: function (userId, docs, fields, modifier) { return (!doc.description || !doc.location.lat || !doc.location.lng || !doc.mysex || !doc.yoursex); } ); 

Note Returning false from deny means giving up the ban. To reject an update, you must return true.

+2
source

All Articles