在express+mongoose项目中使用ES7的async和await

内容导读

不知道我是不是最后一个知道的,反正现在在工程中用的超级爽!不需要bluebird,不需要co,把Promise 的then转化为同步代码简直high翻天。是不是很蛋疼?我竟然这样写完了几乎整个工程。首先是方法名上加async,再就是每个mongoose操作上加await,就变成了同步方法。其次是,不要再每个操作捕捉错误,类似 (err=>{next(err)}) 。

不知道我是不是最后一个知道的,反正现在在工程中用的超级爽!

不需要bluebird,不需要co,把Promise 的then转化为同步代码简直high翻天。。。

要求:

mongoose v>4node v>8

比如这样一个需求:在添加班级时,同步更新学校中的refs,如果用then来写:

 create(req, res, next) { // 本学校下不允许创建重复专业 const major = req.body.major; MajorSchema.findOne({ name: major.name, school: major.school }).then(r => { if (r) { //constructor(message, status = httpStatus.INTERNAL_SERVER_ERROR, isPublic = false) return next( new APIError( '本学校下已有相同名称专业', httpStatus.INTERNAL_SERVER_ERROR, true ) ); } const item = new MajorSchema(major); item._id = mongoose.Types.ObjectId(); item .save() .then(saved => { //更新school中的引用 SchoolSchema.findById(major.school).then(school => { if (school) { if (!school.majors.includes(saved._id)) { school.majors.push(saved._id) SchoolSchema.findByIdAndUpdate(major.school, { majors: school.majors }).then(savedSchools => { res.json(saved); }) } } }) }) .catch(e => next(e)); }); }

是不是很蛋疼?我竟然这样写完了几乎整个工程。。。

昨天发现了async,就象发现了新大陆,使用async-await,写这样的需求:移除专业时,同步移除学校中的refs:

 async remove(req, res, next) { try { const major = await MajorSchema.findByIdAndRemove(req.params.id).exec() //删除学校中的ref if (major) { const school = await SchoolSchema.findById(major.school).exec() if (school && school.majors.includes(major)) { school.majors.splice(school.majors.findIndex(s => s === major._id), 1) const updatedSchool = await SchoolSchema.findByIdAndUpdate(school._id, { majors: school.majors }).exec() res.sendStatus(httpStatus.OK) } } } catch (err) { next(err) } }

首先是方法名上加async,再就是每个mongoose操作上加await,就变成了同步方法。

其次是,不要再每个操作捕捉错误,类似(err=>{next(err)})。这个写 唉

注意一下并不是所有mongoose操作都支持await,只有返回promise的才可以,具体可以看mongoose官网文档:http://mongoosejs.com/docs/


分享到:


相關文章: