Node.js推迟了promisify + mongoose [英] Node.js deferred promisify + mongoose

查看:118
本文介绍了Node.js推迟了promisify + mongoose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人使用nodejs模块'deferred'和'mongoose'?我正在尝试使用deferred.promisify方法来处理mongoose模型的函数,这样我就可以轻松地链接它们,但遇到了一些麻烦。具体来说,我想宣传 find findById 函数,以便我可以链接查找另一个文档引用的一个文档通过ObjectID。

Has anyone worked with the nodejs modules 'deferred' and 'mongoose'? I'm trying to get the deferred.promisify method to work on mongoose models' functions so I can chain them easily, but running into some troubles. Specifically, I'd like to promisify the find and findById functions so I can chain finding one document referenced by another document by ObjectID.

这就是我所拥有的: https:// gist .github.com / 3321827

然而,这似乎不太理想,因为 getAppPermissions getApplication 函数似乎只是 find findById mongoose模型的方法。

However, this seems less than ideal since the getAppPermissions and getApplication functions seem to be little more than wrappers for the find and findById methods of the mongoose model.

我尝试将函数传递给promisify,但是我收到一个关于 Object#< Object>的错误没有方法'_applyNamedScope'这似乎是因为这个不再绑定到正确的对象而引起的。也许我需要使用underscore.bind?有没有人在这方面有任何成功,或者我应该坚持现在的工作?

I tried just passing the functions to promisify, but I get an error about Object #<Object> has no method '_applyNamedScope' which appears to be caused because this is no longer bound to the correct object. Perhaps I need to use underscore.bind? Has anyone had any success in this area, or should I just stick with what's working now?

推荐答案

Mariusz的答案非常接近。希望其他人可以从中学到这一点:

Mariusz's answer was pretty close. Here's what ended up working for me in this particular case, hopefully others can learn from this:

// I put this in my model file so I didn't have to worry about repeating it
var userProto = mongoose.model('User');

userProto.pFind = deferred.promisify(userProto.find);
userProto.pFindOne = deferred.promisify(userProto.findOne);

这篇关于Node.js推迟了promisify + mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆