node.js - TypeError: Cannot call method 'then' of undefined -
i have following code executing inside controller in sailsjs. underlying adapter sails-orientdb. following error back
typeerror: cannot call method 'then' of undefined
why error occurring?
user.query("select id user username='" + req.param("userid") + "'").then(function(userdata){ console.log(userdata); return userdata; }).fail(function (err) { console.log("handled"); });
to expand on @jaumard said.
background
usually it's recommend use standard waterline query methods such find()
, update()
, create()
, destroy()
, etc. these methods support callbacks , promises , these methods work same way against waterline adapter (orientdb, mysql, mongodb, etc).
sometimes may want more complex not supported standard waterline methods , sails-orientdb extends waterline definition query()
, among other methods.
sails-orientdb .query()
sails-orientdb query uses callback can use this:
// assume model named "friend" friend.query("select friendtable name='friend query'", function(err, retrievedusers){ console.log(retrievedusers); });
update: since v0.10.53 below supported:
friend.query("select friendtable name='friend query'") .then(function(retrievedusers){ console.log(retrievedusers); });
if want use promise mechanism have 2 options:
promisify query()
by using bluebird promise's promisify. example:
var querypromise = promise.promisify(user.query); querypromise('select...').then(function(){/*...*/})
use getdb()
another option use sails-orientdb custom method getdb
. method returns oriento db
instance can used promisified queries. example:
user.getdb().query('select ouser name=:name', { params: { name: 'radu' }, limit: 1 }).then(function (results){ console.log(results); });
Comments
Post a Comment