如何在环回远程方法中执行原始 mongo db 查询?我试过:
Members.getDataSource().connector.connect(function (err, db) {
var collection = db.collection('Members');
var res = collection.find();
console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
在这里, res 为我提供了一个对象内的大量数据,但我无法从该对象中找到任何结果文档。任何帮助将不胜感激!谢谢 !
I am developing a NodeJS application using Loopback.
I am pretty new to both nodejs and REST APIs, so please correct me if I am conceptually wrong.
Loopback automatically builds CRUD REST APIs, which is a feature I would like to use in order to avoid to write APIs by myself, but I need to limit users to be able to see only their data.
For example, imagine there are 3 tables in my DB, user, book and a …
我很难找到任何有关重定向到模型函数或remoteMethod. 这里有人已经这样做了吗?请在下面找到我的代码。
Form.catch = function (id, data, cb) {
Form.findById(id, function (err, form) {
if (form) {
form.formentries.create({"input": data},
function(err, result) {
/*
Below i want the callback to redirect to a url
*/
cb(null, "http://google.be");
});
} else {
/*
console.log(err);
*/
let error = new Error();
error.message = 'Form not found';
error.statusCode = 404;
cb(error);
}
});
};
Form.remoteMethod('catch', {
http: {path: '/catch/:id', verb: 'post'},
description: "Public endpoint to create form entries",
accepts: …Run Code Online (Sandbox Code Playgroud) 我是 LoopBack v4 的新手,我正在尝试为每个请求发送一个授权密钥。之前用过Swagger,在点击右侧的“授权”按钮后,我曾经添加了api密钥。为了避免由于我的经验不足而出错,我从“待办事项列表”示例的新应用程序开始。我尝试(但没有成功)遵循这篇文章的建议:Nodejs Loopback 4 add bearer token config into swagger explorer
我所做的是src/index.ts使用以下代码修改文件:
export async function main(options: ApplicationConfig = {}) {
const spec: OpenApiSpec = {
openapi: '3.0.0',
info: {
title: 'LoopBack Application v2',
version: '1.0.2',
},
paths: {
},
securityDefinitions: [
{
api_key: [
{
type: 'apiKey',
name: 'api_key',
in: 'header'
}
]
},
],
};
const app = new TodoListApplication(options);
app.api(spec);
await app.boot();
await app.start();
const url = app.restServer.url;
console.log(`Server is running at ${url}`); …Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来实现与将 unique:true 放入快速模式时相同的功能。我如何在环回 4 中完成此操作。我尝试将 unique true 放入属性装饰器中,但它不起作用。
@property({
type: 'string',
id: true,
required: false,
unique: true,
})
id: string;
Run Code Online (Sandbox Code Playgroud)
这不起作用
我想强制执行唯一性,所以我想看看是否有任何其他电子邮件和用户名与发布到此路由的电子邮件和用户名类似。我如何通过存储库做到这一点,它不断询问我看到但无法理解它的过滤器。
@post('/users', {
responses: {
'200': {
description: 'User model instance',
content: {'application/json': {schema: {'x-ts-type': User}}},
},
},
})
async create(@requestBody() user: User): Promise<User> {
//check : User= await this.userRepository.create(user);
//@param.query.object('filter', getFilterSchemaFor(User)) filter?: Filter;
// var check:any=await this.userRepository.find(filter);
//filter: Filter;
var check: User = await this.userRepository.find({email:user.email});
var isNotPresent: boolean = true;
// check.forEach(function(val){
// });
// if(isNotPresent)
return await this.userRepository.create(user);
}
Run Code Online (Sandbox Code Playgroud) 我注意到一些 TypeScript 节点模块(例如loopback-next/packages)使用节点模块发布它们的源文件。这是否有特殊原因,还是只是不必要地增加了模块的大小?
是否可以在Loopback的远程方法中访问当前用户的角色?
我试图允许远程方法使用where过滤器和当前userId返回基于查找的数据子集,但在admin用户的情况下,我只想返回一整套数据.
所以我一直在尝试获取当前用户的角色列表.但我正在努力使Role.getRoles()返回除以下之外的任何东西:
[ '$unauthenticated', '$everyone' ]
Run Code Online (Sandbox Code Playgroud)
我已经尝试了上下文loopback.getCurrentContext()和传递给beforeRemote方法的上下文,我尝试过ACL.checkAccessForContext().
任何帮助,将不胜感激.
在我的环回应用程序中,一旦我创建了访问令牌(登录后),它在我的应用程序中仍然有效,除非应用程序停止.当应用程序重新启动时,它不允许以前的访问令牌.即使在重新启动应用程序后,如何才能使先前的访问令牌验证?
我对ACL如何在环回上工作有点怀疑.
我正在关注示例https://github.com/strongloop/loopback-example-access-control
REST Api允许create调用将ownerid作为参数传递,但不进行任何验证.
因此,经过身份验证的用户可以创建项目并将ownerid设置为任何值.我认为应该只允许管理员角色设置属性.
我知道我可以放一些代码来进行验证..但我相信必须根据当前登录的用户自动设置值.我错了或者我错过了什么?
谢谢!
loopbackjs ×10
node.js ×4
mongodb ×3
strongloop ×3
express ×2
typescript ×2
v4l2loopback ×2
access-token ×1
javascript ×1
loopback ×1
loopback4 ×1
node-modules ×1
swagger ×1
swagger-ui ×1