根据文档,StrongLoop不支持运行自定义sql语句。https://docs.strongloop.com/display/public/LB/Executing+本机+SQL
我无法理解为什么有人认为你只需简单的连接就可以构建一个企业应用程序,但我确实发现了这篇文章,上面说你可以做到:在MySQL环回连接器上执行原始查询
但这是针对MySql的。当我在Postgres上尝试时,我得到了错误:"类型为'object'的参数'byId'的值无效:0。接收的类型已转换为数字。"它没有返回任何数据。这是我的代码:
module.exports = function(account) {
account.byId = function(byId, cb){
var ds=account.dataSource;
var sql = "SELECT * FROM account where id > ?";
ds.connector.execute(sql, [Number(byId)], function(err, accounts) {
if (err) console.error(err);
console.info(accounts);
cb(err, accounts);
});
};
account.remoteMethod(
'byId',
{
http: {verb: 'get'},
description: "Get accounts greater than id",
accepts: {arg: 'byId', type: 'integer'},
returns: {arg: 'data', type: ['account'], root: true}
}
);
};
对于[Number(byId)]部分,我也尝试过[byId]和byId。什么都不管用。
有什么想法吗?到目前为止,我真的很喜欢StrongLoop,但看起来Postgresql连接器还没有准备好投入生产。如果这不起作用,我下一步将与Sails一起制作原型-(
以下是arg
的类型"integer",它不是有效的环回类型。请改用`Number。检查以下更正的代码:
module.exports = function(account) {
account.byId = function(byId, cb){
var ds = account.dataSource;
var sql = "SELECT * FROM account WHERE id > $1";
ds.connector.execute(sql, byId, function(err, accounts) {
if (err) console.error(err);
console.info(accounts);
cb(err, accounts);
});
};
account.remoteMethod(
'byId',
{
http: {verb: 'get'},
description: "Get accounts greater than id",
accepts: {arg: 'byId', type: 'Number'},
returns: {arg: 'data', type: ['account'], root: true} //here 'account' will be treated as 'Object'.
}
);
};
注意:MySQL的准备语句本机使用?
作为参数占位符,但PostgreSQL使用$1
、$2
等。
希望这对你有用。否则,请根据文档使用[byId]
而不是byId
。