javascript - 远程方法未显示在环回 API 资源管理器中
问题描述
我有一个将 userId 映射到 roleId 的角色映射模型,我需要角色映射模型上的远程方法来检索给定 userId 的角色映射 ID。
这是 remoteMethod 的代码
'use strict';
module.exports = function(Rolemapping) {
Rolemapping.getRolesByUser = async function (id, cb) {
const roleMappings = await Rolemapping.find({ where: { principalId: id
} })
cb(null, roleMappings);
};
Rolemapping.remoteMethod("getRolesByUser", {
http: {
path: "/getRolesByUser",
verb: "get"
},
accepts: [
{ arg: "userId", type: "string", http: { source: "query" } }
],
returns: {
arg: "result",
type: "string"
},
description: "Cvs "
});
};
这是角色映射 json 文件:
{
"name": "roleMapping",
"base": "RoleMapping",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"role": {
"type": "belongsTo",
"model": "role",
"foreignKey": "roleId"
}
},
"acls": [],
"methods": {}
}
上述远程方法未显示在环回 API 资源管理器中。
解决方案
RoleMapping
是一个内置模型,它的role-mapping.js
文件隐藏在 中node_modules/loopback
,我已经测试过了,它看起来不会从common/models
.
看起来引导脚本是您唯一的选择。这是相同的代码,但您的函数接收服务器对象。
server/boot/get-roles-by-user.js
module.exports = function(server) {
const Rolemapping = server.models.RoleMapping;
Rolemapping.getRolesByUser = async function (id) {
return JSON.stringify(await Rolemapping.find({ where: { principalId: id
} }))
};
Rolemapping.remoteMethod("getRolesByUser", {
http: {
path: "/getRolesByUser",
verb: "get"
},
accepts: [
{ arg: "userId", type: "string", http: { source: "query" } }
],
returns: {
arg: "result",
type: "string"
},
description: "Cvs "
});
}
我还cb
从您的远程方法中删除了参数,因为返回 a 的方法Promise
不需要它,只需像返回任何其他函数一样返回值
推荐阅读
- function - 如何在电子表格中从 Yahoo Finance 导入历史数据 (CSV)
- python - 如何从 Seaborn 的计数图图形中删除标签名称?
- javascript - JavaScript:按类单击元素不起作用
- react-native - React Native,使用堆栈导航时如何从 App.js 执行组件的方法
- excel - excel vba循环遍历行并输出到新工作表
- css - 试图让 CSS 网格布局正确
- python - 如何获取列表中所有相同元素的索引?
- css - 边框没有对齐
- javascript - Express API 不会接受来自 POSTMAN 的任何请求
- google-sheets - 如何在谷歌表格中拖动公式?