node.js - Custom Model Validator SailsJS -
when using custom model validator checks record within model. return within spread doesn't seem end control flow.
//post.js model /** * custom validator **/ types: { isuservalid: function(user_id) { var promise = require('bluebird'); promise.all([ user.findone({id: user_id}) ]) .spread(function(user) { console.log(user); if (user === null || user === undefined) { console.log('failed'); return false; }else{ console.log('passed'); return true; } }); } },
my response standard validation failed response.
{ "error": "e_validation", "status": 400, "summary": "1 attribute invalid", "model": "post", "invalidattributes": { "owner": [ { "rule": "isuservalid", "message": "\"isuservalid\" validation rule failed input: 1" } ] } }
looks want "return true , return false" return of isuservalid function. in fact not how javascript scope work. return statement return true inside spread function means promise resolved "true". not returned function. function isuservalid still returns nothing. if function can async promise. should @ least return promise.
return promise.all([....]).spread.....
infact canbe simplified
return user.findone(user_id).then(function(u){....})
however, if function not support promise, meaning synchronous, , expects true/false returned, instead of promise object. have make process synchronous. 1 way convert promise synchronous using generator function. see here
Comments
Post a Comment