nodejs 모듈 '지연'과 '몽구스'로 작업 한 사람이 있습니까? mongoose 모델의 함수를 처리하기 위해 deferred.promisify 메서드를 얻으려고 노력하고 있으므로 쉽게 연결할 수 있지만 문제가 발생합니다. 특히, 나는 find
과 findById
함수를 약속하고 싶습니다. 그래서 ObjectID에 의해 다른 문서가 참조하는 하나의 문서를 찾을 수 있습니다.Node.js 지연 약속 + 몽구스
는 여기에 내가있어 무엇 다음 getAppPermissions
및 getApplication
기능은 몽구스 모델의 find
및 findById
방법의 래퍼 인 것 같다부터 https://gist.github.com/3321827
그러나,이 이상적이지 보인다.
나는 promisify 함수를 전달하려고 시도했지만 이 더 이상 올바른 개체에 바인딩되지 않아서 발생하는 것으로 보이는 Object #<Object> has no method '_applyNamedScope'
에 대한 오류가 발생합니다. 아마 밑줄을 써야 겠어. 묶어 줘? 누구든지이 분야에서 성공을 거둔 적이 있습니까? 아니면 지금 일하고있는 것을 고수해야합니까? 또한 프로토 타입을 오염 자제 할 수
// I assume that methods you're using are set on mongoose.Model,
// but be sure to check, maybe, they're using some other prototype (!)
var promisify = require('deferred').promisify;
var modelProto = mongoose.Model.prototype;
modelProto.pFind = promisify(modelProto.find);
modelProto.pFindById = promisify(modelProto.findById);
// After that you may use promisified methods directly:
app.get('/apps', requireLogin, function (req, res) {
AppPermissions.pFind({ user: req.user.id, valid: true })
.map(function (permission) {
return ApplicationRecord.pFindById(permission.application)(
function (application) {
application.permission = permisson;
return application;
}
);
}).end(function (applications) {
res.render('applist', { applications: applications });
}, null);
});
및 사용 : 당신은 아마 제대로 방법에 컨텍스트를 통과하지 못한
노드/V8은'Function.prototype.bind' 구현, 당신은 밑줄의 결합을 필요가 없습니다; 예를 들어,'MyModel.find.bind (MyModel)'. –