javascript - 远程方法未显示在环回 API 资源管理器中

标签 javascript node.js loopbackjs

我有一个 Angular 色映射模型,它将 userId 映射到 roleId,我需要 Angular 色映射模型上的远程方法来检索给定 userId 的 Angular 色映射 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 "
  });
 };

这是 Angular 色映射 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 为自己加载 js 文件。

看来启动脚本是您唯一的选择。这是相同的代码,但您的函数接收服务器对象。

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 参数,因为返回 Promise 的方法不需要它,只需像返回任何值一样返回值其他功能

关于javascript - 远程方法未显示在环回 API 资源管理器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54088377/

相关文章:

javascript - 在 if 语句中,undefined 等于 false

javascript - 尝试创建队列时出现 ChromeCast session_error

node.js - 将 AWS Lambda 函数转换为使用 promise ?

loopbackjs - 对类型为其他模型的属性进行环回验证(版本 2.xx)

node.js - SLC环回: Using model instance from within a model hook

javascript - 在 Backbone 中表示任意深度嵌套列表的最简单方法

javascript - redux 的一般用法

node.js - 如何在一台VPS服务器上运行多个独立的Loopback应用程序?

ruby-on-rails - 桥接一个简单的 Node.js 和 Socket.io 聊天应用程序与 Rails 应用程序(在 Heroku 上)

javascript - 如何在nodejs中提供文件之前保存变量