与loopback中的where子句一起使用时,Count方法抛出语法错误

时间:2015-05-07 07:07:37

标签: node.js loopbackjs strongloop

我在node.js中使用loopback / strong循环。 我试图在count方法中使用where子句用于分页目的。每当我尝试使用where子句时,它会给出一个mysql语法错误,如下所示的简单查询。

Ride.count({
where:{"id":20}
},function(err,totalCount){
if (err) {
log.info("Total error ", err);
fn(err);
}else {
log.info("Total count ", totalCount);
}
});

这是我得到的错误。

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '20' at line 1\n at Query.Sequence._packetToError (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)\n at Query.ErrorPacket (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\sequences\Query.js:83:18)\n at Protocol._parsePacket (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\Protocol.js:271:23)\n at Parser.write (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\Parser.js:77:12)\n at Protocol.write (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\protocol\Protocol.js:39:16)\n at Socket. (D:\Code\liftee\rest-services\node_modules\loopback-connector-mysql\node_modules\mysql\lib\Connection.js:92:28)\n at Socket.emit (events.js:107:17)\n at readableAddChunk (_stream_readable.js:163:16)\n at Socket.Readable.push (_stream_readable.js:126:10)\n at TCP.onread (net.js:529:20)\n --------------------\n at Protocol._enqueue (D:\Code\liftee\rest-services\node_modules\loopback-co

1 个答案:

答案 0 :(得分:4)

尝试不使用where子句。我已经在我的模型上测试了这个并且它有效。

Ride.count(
  {"id":20},
  function(err,totalCount){
    if (err) {
      log.info("Total error ", err);
      fn(err);
    }else {
      log.info("Total count ", totalCount);
    }
  }
);

查看strongloop API资源管理器。您只需要将值传递给count函数。