简体   繁体   中英

Multiple Meteor.Error

I'm just wondering on how to throw multiple Meteor.Errors for my validation fields like

throw new Meteor.Error('403','Invalid Input','username Id');
throw new Meteor.Error('403','Too Short','password Id');

and throw them at the same time to client.

I'd take an approach like this:

var errors = [];
if (/* password is too short */) {
  errors.push("password is too short");
}
if (/* username is invalid */) {
  errors.push("username is invalid");
}
// ...

if (errors.length > 0) {
  throw new Meteor.Error(403, errors.join("; "));
}

Achieved what I want by making two empty arrays,pushing values if an error is detected, wrapping them up to throw to the client and iterating those values in the client's ERROR callback.

//SERVER
var reason=[],ids=[]; 

  if(error){
     reason.push('error details');
     ids.push('id of the element');
  }

 if(reason.length!==0){
   throw new Meteor.Error('403',reason,ids);
 }


//CLIENT
Accounts.createUser({foo:bar,boo:baz}, function(error){
  if(error){
  _.each(error.details,function(index,element){
        and there your client error code goes
    });
  }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM