Skip to content

Instantly share code, notes, and snippets.

@Suave
Created December 4, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Suave/5cfa68108a864110d5de to your computer and use it in GitHub Desktop.
Save Suave/5cfa68108a864110d5de to your computer and use it in GitHub Desktop.
mongoose validator error async issue
exports.create = function*(){
var body = this.request.body,
name = body.name,
type = body.shop_type,
context = body.context;
/*
this.checkBody("name").notEmpty("请输入商户名称");
this.checkBody("shop_type").notEmpty("请输入原料名");
this.checkBody("context").notEmpty("请输入口味描述");
if(this.errors){
return this.body = {retcode:1 , message:'请输入完整信息'};
}
*/
var checkShop = yield Shop.model.findOne({name:name}).exec();
if(checkShop != null){
return this.body = {retcode:1,message:'已存在相同的商户'}
}
var shopData = {};
shopData.name = name;
shopData.type = type;
shopData.context = context;
try{
var newShop = new Shop.model(shopData);
yield newShop.save(function(err){
if(err) {
return this.body = {retcode: 1, message: err.errors};
} else {
return this.body = {retcode: 0}
}
});
} catch(ex) {
}
/*
var result = yield (function(newShop){
return function(fn){newShop.save(fn)};
})(newShop);
return this.body = {retcode:0}
*/
return this.body = {retcode: 0}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment