Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {
  console.log(user);
});

but this one won't

User.query("SELECT email FROM user WHERE email = ?", [ email ]))
.then(function(err, rows) {
  console.log(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

like image 660
Yann Pellegrini Avatar asked Feb 19 '14 16:02

Yann Pellegrini


1 Answers

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) {
    console.log(user);
});
like image 188
Steve Kehlet Avatar answered Nov 01 '22 21:11

Steve Kehlet