我有一个问题我在Loopback的文档中找不到答案.
说我有模特Company和模特Employee.它Company和它之间有1Xn的关系Employees.当/api/Employees被调用时,服务器将返回所有员工.
我只想返回与请求列表的用户在同一公司的员工列表.
为此,我创建了一个远程钩子
Employee.beforeRemote('find', function(context, modelInstance, next) {
var reject = function() {
process.nextTick(function() {
next(null, false);
});
};
// do not allow anonymous users
var userId = context.req.accessToken.userId;
if (!userId) {
return reject();
}
//I get the details of the user who sent the request
//to learn which company does he belong to
Employee.findById(userId, function(err, user) {
if(!context.req.query.filter) context.req.query.filter={};
context.req.query.filter.where = {brandId:user.companyId};
console.log(context.req.query);
next();
});
});
Run Code Online (Sandbox Code Playgroud)
我认为这应该每次都有用,但是看起来它只有当find已经有一些像include这样的查询过滤器时才会起作用 - …
以下是有关AngularJS JavaScript SDK的文档
这个例子适用于Angular.可以使用命令生成Angular客户端库
$ lb-ng ../server/server.js js/lb-services.js
Run Code Online (Sandbox Code Playgroud)
是否存在将Angular2与Loopback一起使用的简单方法?
编辑
我目前在这个主题上发现了什么.
编辑
我正在使用nodejs的loopback框架.
是否可以一次连接多个数据库.
例如,我有两个不同的数据库.
1. Mysql Database - A
2. Postgresql - B
Run Code Online (Sandbox Code Playgroud)
有些页面从A数据库获取数据,有些页面需要从B数据库获取数据.有可能这样做吗?
更多细节:
假设我们有两个模块.一个模块与MySQL交互,另一个模块与postgreSQL交互.
我有一个问题,当我将两个对象类型作为远程方法参数传递时,第一个参数被第二个参数覆盖.以下是代码和结果.我怎么能不让第二个参数不覆盖第一个参数呢?
module.exports = (Model) => {
Model.calculate = (primary, secondary) => {
console.log(JSON.stringify(primary, null, 2));
console.log(JSON.stringify(secondary, null, 2));
return new Promise((resolve, reject) => {
resolve({ Model: calculator.calculate() });
});
};
Model.remoteMethod('calculate', {
accepts: [
{ arg: 'primary', type: 'object', http: { source: 'body' } },
{ arg: 'secondary', type: 'object', http: { source: 'body' } }
],
returns: {arg: 'Result', type: 'string'}
});
};
Run Code Online (Sandbox Code Playgroud)
当我在控制台记录JSON对象primary和secondary后传入主参数{"name":"Tom"}和辅助参数{"name:"Joe"}时,我得到了结果.
primary
{
"name": "Joe" <--- WHY?!
}
secondary
{
"name: "Joe"
}
Run Code Online (Sandbox Code Playgroud)
如你所见,汤姆被乔覆盖了.
我正在使用Loopback为我的服务器进行以下设置:
"loopback": "^2.36.0",
"loopback-boot": "^2.23.0",
"loopback-component-explorer": "^2.4.0",
"loopback-component-storage": "^1.9.1",
"loopback-connector-mongodb": "^1.17.0",
"loopback-datasource-juggler": "^2.53.0",
Run Code Online (Sandbox Code Playgroud)
它连接到mlab.com上的mongoDB
我的主要查询包括一个模型及其依赖项,例如:
GET /api/course/1234
PARAMS filter = {
include: [
{relation:'students'},
{relation:'documents'},
{relation:'modules'}
]
}
Run Code Online (Sandbox Code Playgroud)
99.9%的时间,它完美地运作.但是当我开始在平台上开展更多活动时,我会在服务器开始崩溃之前开始收到此错误.这是错误:
MongoError: not authorized for query on db.student
at Function.MongoError.create (/app/node_modules/mongodb-core/lib/error.js:31:11)
at queryCallback (/app/node_modules/mongodb-core/lib/cursor.js:182:34)
at /app/node_modules/continuation-local-storage/context.js:76:17
at bound (domain.js:287:14)
at runBound (domain.js:300:12)
at Callbacks.emit (/app/node_modules/mongodb-core/lib/topologies/server.js:119:3)
at null.messageHandler (/app/node_modules/mongodb-core/lib/topologies/server.js:397:23)
at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:302:22)
Run Code Online (Sandbox Code Playgroud)
有时它会打开db.students,有时候它会db.documents......似乎是随机的,我不知道如何修复它.
要连接到DB,我使用mlab提供的URI .看起来像这样:
mongodb://<dbuser>:<dbpassword>@<dbpath>/<dbname>
Run Code Online (Sandbox Code Playgroud)
有和没有?&authMode=scram-sha1&rm.tcpNoDelay=true,但仍然有错误.
任何想法发生了什么以及如何解决这个问题?
__编辑:我尝试了几个不同的版本而没有运气.最新的是:
"loopback": "^2.22.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用环回2.0向我的api添加get远程方法,以实现与默认方法相同的方法结构,例如:
/myObject/{id}
Run Code Online (Sandbox Code Playgroud)
我尝试过的方法是:
MyObject.remoteMethod(
'remotemethod', {
http: {
path: '/',
verb: 'get'
},
accepts: [
{arg: 'id', type: 'string'},
],
returns: {
arg: 'status',
type: 'string'
}
}
)
Run Code Online (Sandbox Code Playgroud)
但这只允许我这样做:
http://localhost:3000/api/myObject?id=1
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能做到这一点?
有人还知道我如何向该路线添加说明以在浏览器中显示吗?文档并没有真正说明太多。.我认为他们的文档不完整,我是唯一一个有这种想法的人吗?
有些问题我在文档中找不到答案.
我想要得到这样的结构:
Node:
id: '1sdf12asd123',
name: 'node1',
history:
[ ts: 234234234234,
data: { 'foo': 'bar' }
],
...
Run Code Online (Sandbox Code Playgroud)
所以每个单独的节点都有很多历史项.而且我希望能够在push不覆盖任何内容的情况下获得新数据.
现在,我不希望将所有内容存储在每个节点下,而是存储在单独的文档中,因此我认为embedsMany适合于此:
{
"name": "Node",
"plural": "Nodes",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {
"history": {
"type": "embedsMany",
"model": "History",
"foreignKey": "HistoryId"
}
},
"acls": [],
"methods": {}
}
Run Code Online (Sandbox Code Playgroud)
所以历史可以简单地说:
{
"name": "History",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"ts": {
"type": "Date"
},
"data": { …Run Code Online (Sandbox Code Playgroud) 我在react-redux&API app上有一个客户端应用程序loopback.
对于我的本地测试,我在端口8080上运行客户端应用程序,在端口3000上运行服务器应用程序
当我尝试使用loopback-passport component客户端应用测试Google OAuth(使用)时,我收到以下错误.
当我使用POSTMAN测试它时,没有问题.
这是客户端代码,
require('babel-polyfill');
import { CALL_API } from 'redux-api-middleware';
import C from '../constants';
const API_ROOT = 'http://localhost:3000';
function googleLogin() { return async(dispatch) => {
const actionResp = await dispatch({
[CALL_API]: {
endpoint: API_ROOT + '/auth/google',
headers: {
'Access-Control-Allow-Credentials': 'false',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin': API_ROOT
},
method: 'GET',
types: ['GET', 'GET_SUCCESS', 'GET_FAILED']
}
});
if (actionResp.error) {
console.log(actionResp);
throw new Error('Some error in communication', actionResp);
} else {
console.log(actionResp);
} …Run Code Online (Sandbox Code Playgroud) 嗨,作为问题的标题,我想知道如何在启动测试之前检查环回启动脚本是否已完成.在示例项目中:
https://github.com/strongloop/loopback-example-relations
测试文件夹中有一个文件似乎可以完成这项工作,但遗憾的是它没有解决它.
start-server.js:
var app = require('../server/server');
module.exports = function(done) {
if (app.loaded) {
app.once('started', done);
app.start();
} else {
app.once('loaded', function() {
app.once('started', done);
app.start();
});
}
};
Run Code Online (Sandbox Code Playgroud)
此脚本在其余测试API中加载,如下所示:
before(function(done) {
require('./start-server');
done();
});
Run Code Online (Sandbox Code Playgroud)
但是从不调用该函数.这是使用该脚本的正确方法吗?
我结束了以下实现:
before(function (done) {
if (app.booting) {
console.log('Waiting for app boot...');
app.on('booted', done);
} else {
done();
}
});
Run Code Online (Sandbox Code Playgroud)
哪个有效,但我对这个启动服务器脚本感到困惑.
按照@stalin建议编辑我修改了before函数如下:
before(function(done) {
require('./start-server')(done);
});
Run Code Online (Sandbox Code Playgroud)
并且执行进入else分支但从done未被调用.
我正在使用access_token处理逻辑表单环回.它工作正常,但不幸的是期望URL中的access_token.
我可以配置环回来使用标头自定义字段中的access_token吗?
loopbackjs ×10
javascript ×5
node.js ×5
strongloop ×5
angular ×1
arguments ×1
cors ×1
mlab ×1
mongodb ×1
mysql ×1
object ×1
overwrite ×1
postgresql ×1
reactjs ×1
redux ×1