Model.query()返回“undefined”错误

在Sailsjs控制台上,我试图在MySQL数据库上使用Waterline / Sails的query()方法执行原始的SQL查询。 但是,我不断收到错误消息:

 sails> User.query('SELECT id from user').exec(console.log) TypeError: Cannot call method 'exec' of undefined at repl:1:37 at REPLServer.self.eval (repl.js:112:21) at Interface. (repl.js:239:12) at Interface.emit (events.js:117:20) at Interface._onLine (readline.js:203:10) at Interface._line (readline.js:532:8) at Interface._ttyWrite (readline.js:761:14) at ReadStream.onkeypress (readline.js:100:10) at ReadStream.emit (events.js:98:17) at emitKey (readline.js:1096:12) 

运行Waterline的其他方法,如find()成功执行。

 sails> User.find().exec(console.log) undefined sails> null [ { email: 'xxxx', password: 'xxxxx', name: 'xxxx', }]
sails> User.find().exec(console.log) undefined sails> null [ { email: 'xxxx', password: 'xxxxx', name: 'xxxx', }] 

互联网search表明, sails-mysql适配器应该用于连接; 这就是我正在使用的问题仍然存在。

我的config / connections.js文件

 module.exports.connections = { default_dev_mysql_server: { adapter: 'sails-mysql', host: '127.0.0.1', port: 3306, user: 'xxxxx', password: 'xxxxx', database: 'xxxxxx'} }
module.exports.connections = { default_dev_mysql_server: { adapter: 'sails-mysql', host: '127.0.0.1', port: 3306, user: 'xxxxx', password: 'xxxxx', database: 'xxxxxx'} } 

我的config / models.js文件

 module.exports.models = { schema: true, connection: 'default_dev_mysql_server', migrate: 'safe' };
module.exports.models = { schema: true, connection: 'default_dev_mysql_server', migrate: 'safe' }; 

我正在运行sails-mysql v0.11.2sails v0.11.2 ,这是最稳定的软件版本。

我错过了什么其他设置能够在Sailsjs上执行原始SQL查询? 谷歌并没有太大的帮助。

谢谢。

exec在使用ORM特性时是必须的,因为在内部,它会build立一个SQL语句,在稍后的阶段被执行。 query另一方面,接受一个stringSQL语句和一个callback作为它的参数,并没有用于exec ,因此它的未定义。 试试这个:

 User.query('SELECT id from user', function(err, res){ console.log(err, res); });