【问题标题】:Model.query() returns "undefined" errorModel.query() 返回“未定义”错误
【发布时间】:2015-12-30 03:02:14
【问题描述】:

在 Sailsjs 控制台上,我正在尝试使用 Waterline/Sails 的 query() 方法在 MySQL 数据库。但是,我不断收到错误消息:

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)

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

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

Internet 搜索表明应该使用 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'} }

我的 config/models.js 文件

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

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

为了能够在 Sailsjs 上执行原始 SQL 查询,我还缺少哪些其他设置? Google 的帮助不大。

谢谢。

【问题讨论】:

    标签: javascript node.js sails.js


    【解决方案1】:

    exec 在使用 ORM 功能时是必需的,因为它在内部构建了一个 SQL 语句以在稍后阶段被 execcuted。另一方面,query 接受一个字符串 SQL 语句和一个回调作为其参数,并且对 exec 没有用处,因此它是未定义的。试试这个:

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

    【讨论】:

    • 哇!如果我更多地注意这些示例,我实际上可以看到 exec() 没有与 query() 一起使用,而它用于其他功能。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 2017-12-06
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多