YannP YannP - 1 year ago 180
Node.js Question

How to use Model.query() with promises in SailsJS/Waterline?

I'm having issues with Sails.JS 0.9.8.
I would like to use promises with the Model.query() function (I use sails-mysql adapter).

This code will work :

User.findOne({ email: email })
.then(function(user) {

but this one won't

User.query("SELECT email FROM user WHERE email = ?", [ email ]))
.then(function(err, rows) {

I get undefined for both 'err' and 'rows'.

Is it just not implemented or I am doing something wrong ?
If not implemented, is there any alternative to use promises with .query() ?

Thank you in advance

Answer Source

You can promisify(User.query) yourself, just like you'd do for any other callback-based API, like:

var Promise = require('bluebird');


var userQueryAsync = Promise.promisify(User.query);
userQueryAsync("SELECT email FROM user WHERE email = ?", [ email ])
.then(function(user) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download