我正在使用loopback 2.0.
我使用yeoman生成器生成了我的模型,并为每个模型添加了一个js文件以扩展其行为.
如何在ModelB中调用ModelA中的方法?
例
文件夹结构:
/common
/models
Car.json
Car.js
Engine.json
Engine.js
...
Run Code Online (Sandbox Code Playgroud)
Car.js:
module.exports = function(Car) {
Car.drive = function(destination, fn) { ... }
...
};
Run Code Online (Sandbox Code Playgroud)
的engine.js:
module.exports = function(Engine) {
Engine.doSomething = function(something, fn) {
// *** Here is where I want to invoke a method from the Car.js
var loopback = require('loopback');
var Car = loopback.models.Car;
Car.drive('49.1234,12.1234', fn);
// ***
}
...
};
Run Code Online (Sandbox Code Playgroud) 我需要找到一种方法来更改从自定义远程方法发送的响应的内容类型。看来默认情况下它是application/json。
我有一个返回图像的远程方法,因此我需要以某种方式更改内容类型。
我们将LoopBack用于REST API.我们注意到REST端点返回JSON的不一致.我们可以编写代码,但最好知道如何在我们的代码中正确解决它.问题是当标准REST端点返回JSON时,没有JSON对象ID.但是,当我们从直接执行的查询返回JSON时,我们必须指定一个对象ID.这是从标准LoopBack REST'get'调用返回的JSON:

当我们返回自己的JSON时,它似乎必须有一个ID:

这是我们如何返回自己的JSON(TypeScript):
var sql: string = "SELECT * FROM as_trialbalancelist"
// Execute the SQL
dbConn.query(sql, function (error: number, rows: any, fields: any) {
// Check for errors
if (error) {
console.log('ERROR: ' + error);
// TODO: Need better error return.
// res.send(error);
return;
}
callback(null, rows);
});
Dataview.remoteMethod(
'trialbalance',
{
returns: { arg: 'data' },
http: { verb: 'get' }
}
);
Run Code Online (Sandbox Code Playgroud)
当我们删除"returns:{arg:'data'}"时会导致错误.空白'arg'也会导致问题.
我在 node.js 中使用环回/强循环。我正在尝试在 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);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误。
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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试环回,并注意到在model-config文件中它引用了5个模型,它们dataSource设置为内存数据库db:
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
}
Run Code Online (Sandbox Code Playgroud)
是否需要将用户,acl等保留在内存中?如果没有,我该如何将这些模型移植到mysql?我尝试切换dataSource到我的mysql源,但服务器抱怨,因为表不存在.
根据文档,StrongLoop不支持运行自定义sql语句。 https://docs.strongloop.com/display/public/LB/Executing+native+SQL
任何人都认为您可以通过简单的连接就可以构建企业应用程序,这超出了我,但是我确实找到了这篇文章,说明您可以做到: 在MySQL Loopback Connector上执行原始查询
但这是针对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}
}
);
};
Run Code Online (Sandbox Code Playgroud)
对于[Number(byId)]部分,我也尝试过[byId]和仅byId。没用。
有任何想法吗?到目前为止,我真的很喜欢StrongLoop,但是看来Postgresql连接器尚未准备好投入生产。如果不行,我将在下一个Sails中制作原型。:-(
我克隆了一个https://github.com/beeman/loopback-angular-admin
,我使用环回资源管理器创建了几个新角色,但是如何为我创建的用户分配角色
我有一个用户模型,它在环回中从User模型扩展而且模型文件是这样的 -
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "accessToken",
"foreignKey": "userId"
},
"identities": {
"type": "hasMany",
"model": "userIdentity",
"foreignKey": "userId"
},
"credentials": {
"type": "hasMany",
"model": "userCredential",
"foreignKey": "userId"
},
"roles": {
"type": "hasMany",
"model": "Role",
"foreignKey": "principalId",
"through": "RoleMapping"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "READ",
"principalType": …Run Code Online (Sandbox Code Playgroud) 对于持久化模型,使用strongloop的loopback默认REST API,执行以下操作之间的区别是什么:
HEAD Ressource/{id}GET Ressource/{id}/exists?
在我的一个LoopBack模型中,我想向模型添加一个实例方法,但它不会让我访问this,因为this在prototype方法中未定义:
module.exports = (MyModel) => {
MyModel.prototype.doStuff = () => {
console.log(this); // outputs undefined
}
}
Run Code Online (Sandbox Code Playgroud)
这显然限制了实例方法的有用性.有没有办法做到这一点?
我正在尝试获得一个产品,包括它所属的类别.课程类别包含许多产品,产品属于同一类别.我是这样做的.
Product.find({include: 'Categories'})
Run Code Online (Sandbox Code Playgroud)
当我尝试执行此查询时,出现错误."关系"类别"未定义为产品型号".
我根据这里找到的环回文档定义了这种关系:https://docs.strongloop.com/display/public/LB/HasMany+relations.由于一个类别有很多产品.看起来如下:
category.json
"relations": {
"products": {
"type": "hasMany",
"model": "Product",
"foreignKey": "categoryId"
}
},
Run Code Online (Sandbox Code Playgroud)
products.json中没有定义任何内容.我已经检查了复数当然,但他们都检查了.
当我尝试获取所有类别的所有产品时,它的工作没有问题.我很确定这是有意的.但我怎么能这样做呢?我已经尝试在Product中定义一个关系,如下所示:https ://docs.strongloop.com/display/public/LB/HasOne+relations .但这需要在另一个对象中使用外键,因此显然只是用于一对一的关系.除此之外,我有点难过......有什么想法吗?
strongloop ×10
loopbackjs ×9
node.js ×3
express ×2
angularjs ×1
instance ×1
javascript ×1
json ×1
methods ×1
postgresql ×1
roles ×1