New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hello #1
Comments
Hello! I'm working on a relatively big project and I'm kind of trying to avoid But yeah... That seemed as if it would work fine. This specific case doesn't seem to work properly though. I'm still trying to Here's a reproduction: var Promise = require('bluebird');
var mongoose = require('mongoose');
var UserSchema = new mongoose.Schema({
name: { type: String }
});
var User = mongoose.model('User', UserSchema);
User = Promise.promisifyAll(User);
Promise.promisifyAll(User.prototype); This last line throws:
Haha... This is actually a much more interesting problem than what I started with... I actually tried promisifying other parts of the Model, but I can't seem to get it to work. I'll look more into this later today or tomorrow morning. |
Wow very good find. I have fixed this bug in: petkaantonov/bluebird@beb26fb which is in 0.10.11 It should now work: Promise.promisifyAll(User.prototype);
console.log(User.prototype.getAsync);
{ [Function: getAsync] __isPromisified__: true } |
Seems ###*
* Fix broken function after mongoose model is promisified by bluebird
* @param {Model} model mongoose model to fix
###
exports.fixPromisify = (model) ->
# Somehow, saveAsync doesn't trigger validation, so run validateAsync first
originalSaveAsync = model.prototype.saveAsync
model.prototype.saveAsync = ->
@validateAsync().then =>
originalSaveAsync.call(this) Any advice? |
I don't see how Anyway, if there is problem it's somewhere else |
Validation is run at a pre 'save' hook But that's weird, saveAsync would call Maybe hooks-js breaks with this, but I don't have this problem, so I don't think that's the case. |
With the hack I mentioned previously, validation works, but ###*
* Promisify model with bluebird
* @param {Model} model mongoose model to promisify
###
exports.promisifyModel = (model) ->
Promise.promisifyAll model
Promise.promisifyAll model.prototype
# Somehow, saveAsync doesn't trigger validation, so run validateAsync first
# FIXME: other hooks(pre/post) still can not be triggered
originalSaveAsync = model.prototype.saveAsync
model.prototype.saveAsync = ->
@validateAsync().then =>
originalSaveAsync.call this |
@inetfuture @petkaantonov I made a bunch of experiments to try to understand it and found the problem... The problem is This wouldn't be a problem if bluebird wrapped the Removing this line fixes the problem, but I understand why it's there. The solution would be to make a custom promisification function which calls the original methods rather then the "beforePromisified" versions. |
@yamadapc Sorry, I can't totally get what you're saying, have you figure out a fix for this? |
If a class adds methods in their constructor then you need to promisifyAll the created object after the constructor is called. |
Ok, now I understand this part
Still looking into this:
|
@petkaantonov |
The reason for the key suffix is because an object might contain methods that end in Async but are not promisified. See https://gist.github.com/spion/a535623f4c1a779339b9 I suppose promisifyAll weakness could be changed from "not working with methods that are overriden in constructor" to "not working with objects that have methods ending in Async" |
@petkaantonov I'd say this is a decent tradeoff. Perhaps adding a modified version of If you think that's a good idea, I'm wiling to write and PR it. |
What I meant was that the Get it? I'm sorry... I know this is confusing and my english probably doesn't help much. |
@yamadapc I don't think the edge case is worth any additional code and especially API surface like optional param. I mean you need a class with methods named 'x' and 'xAsync' to cause a problem, and I really don't see that as realistic. I will provide a fix tonight - this actually requires quite a bit of modifications in the ugliest part of the code base :P |
@yamadapc Now I get it, thanks! |
@petkaantonov did you commit a change to fix this, or should we promisify on the instance as suggested ? |
@wprater petkaantonov/bluebird@70f90c3 this should work now, as expected. |
nice discussion, i have been working on this all day, and finally got a solution |
@simongfxu The latest bluebird improved this functionality and now all you have to do is https://github.com/petkaantonov/bluebird/blob/2.0/API.md#promisification |
@jeduan As far as I'm aware, to promisify This is due to the fact that the var Aggregate = require('mongoose/lib/aggregate');
var Promise = require('bluebird');
Promise.promisifyAll(Aggregate.prototype);
// etc.
SomeModel.aggregate(/* etc. */).execAsync(); If you're not manipulating instances of |
@jeduan thanks,that is so nice. |
I stumbled upon here and wanted to ask for feedback as one goal of bluebird is to make wrapping extremely trivial and thus utilities less necessary :).
I was wondering why the following wouldn't work for you:
The text was updated successfully, but these errors were encountered: