I have the following code executing inside a controller in Sailsjs. The underlying adapter is sails-orientdb. I get the following error back
TypeError: Cannot call method 'then' of undefined
Why is this error occurring?
User.query("select id from User where username='" + req.param("userid") + "'").then(function(userData){
console.log(userData);
return userData;
}).fail(function (err) {
console.log("Handled");
});
To expand on what @jaumard said.
Background
Usually it's recommend to use the standard waterline query methods such as
find()
,update()
,create()
,destroy()
, etc. These methods support callbacks and promises and these methods will work the same way against any waterline adapter (OrientDB, MySQL, MongoDB, etc).Sometimes you may want to do something more complex which is not supported by the standard waterline methods and for that sails-orientdb extends the waterline definition with
query()
, among other methods.sails-orientdb
.query()
sails-orientdb query uses callback so you can use it like this:
UPDATE: since v0.10.53 the below is also supported:
If you really want to use the promise mechanism you have 2 options:
promisify
query()
By using bluebird promise's promisify. Example:
Use
getDB()
Another option is to use sails-orientdb custom method
getDB
. This method returns an Orientodb
instance which can be used for promisified queries. Example: