0

Assume I have the following function:

Person.find({ surname: "Doe" }).only("name", "surname").run(function (err, people) {

});

This is what I have tried using promises:

Q.nfcall(Person.find.only('name', 'surname').run, {surname: 'Doe'});

Where should I insert arguments for the only() function using promises?

Thank you

2

2 Answers 2

3

First of all, if you're using Node.js 0.12+ or io.js, you don't need to use Q or other libraries to support promises, as they're available natively.

Second, it seems like you're using Mongoose, which support promises natively: http://mongoosejs.com/docs/harmony.html

When you call Model.find(), it returns a promise already, so you can just do:

var promise = Person.find({ surname: "Doe" }).only("name", "surname")
promise.then(...)
Sign up to request clarification or add additional context in comments.

Comments

2

The arguments for only should go where they always went, just as well as those to find. It's only run that you want to call with a callback and get a promise out:

Q.nfcall(Person.find({ surname: "Doe" }).only("name", "surname").run)
.then(function(people){…}, function(err) {…});

But probably you'll have to use Q.ninvoke anyway:

Q.ninvoke(Person.find({ surname: "Doe" }).only("name", "surname"), "run")
.then(function(people){…}, function(err) {…});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.