【问题标题】:CRUD methods and endpoints for RESTful api not being generated in proxy api server代理 api 服务器中未生成 RESTful api 的 CRUD 方法和端点
【发布时间】:2016-10-25 06:23:05
【问题描述】:

我正在尝试代理服务器(http://www.swapi.co/api/starships 用于练习,然后是 Salesforce api 用于生产)。这将是 React Native 应用程序的移动后端。我在这里关注文档:http://loopback.io/doc/en/lb2/REST-connector.html#resource-operations。但是,当使用生成器创建使用 CRUD 操作和 Starship 模型的“starship”数据源时,当我尝试使用内置资源管理器探索 api 时,什么也没有显示。代理 RESTful API 时,我想使用 RESTful API 公开它,这是否还可以用于 Loopback?

这是我用来查看 api 的资源管理器的屏幕截图:

以下是我正在采取的步骤:

$ slc loopback:datasource
? Enter the data-source name: starship
? Select the connector for starship: REST Services (supported by StrongLoop)
? Base URL for the REST service: http://www.swapi.co/api/starship
? Default options for the request: [left blank, hit enter]
? An array of operation templates: [left blank, hit enter]
? Use default CRUD mapping: (y/N) Y

$ slc loopback:model
? Enter model name: Starship
? Select data-source to attach Starship to: starship (rest)
? Select model's base class: Model
? Expose Person via the REST API? (Y/n) Y
? Custom plural form (used to build REST URL): starships
? Common model or server only? common

Let's add some Starship properties now.

Enter an empty property name when done.
? Property name: [left empty, hit enter]

$ npm start

> wfsapi@1.0.0 start /Users/me/projects/wfsapi
> node .

Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer

但是,当我导航到资源管理器时,只显示用户 api,而 Starships 则没有。关于我可能做错了什么的任何想法?以下是我能找到的生成文件的内容:

common/models/starship.js

'use strict';

module.exports = function (Starship) {

};

common/models/starship.json

{
    "name": "Starship",
    "plural": "starships",
    "base": "Model",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "properties":  {},
    "validations":  [],
    "relations": {},
    "acls": [],
    "methods": {}
}

服务器/datasources.json

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "starship": {
    "name": "starship",
    "baseURL": "http://www.swapi.co/api/starships",
    "crud": true,
    "connector": "rest"
  }
}

服务器/model-config.json

{ 
  ... 
  "Starship": {
    "datasource": "starship",
    "public": true
  }
}

【问题讨论】:

  • 你试图打开什么 url 来探索你的 api?您可以发布图片或详细说明显示的错误吗?
  • 我刚刚添加了一个 Loopback explorer api 的屏幕截图,我在运行 api 服务器后导航到该 api。服务器运行的服务器控制台中没有错误,它只是没有为模型生成 CRUD 方法。我不确定应该如何使用生成器来生成使用 loopback-rest-connector 连接到 3rd 方 api 的 CRUD 模型。我一定做错了什么,但我只是没有看到。您是在询问服务器终端中的错误还是浏览器控制台中的错误?
  • 我刚查了一下,浏览器控制台也没有错误。

标签: javascript rest api loopbackjs


【解决方案1】:

问题是,您使用 Model 作为基础而不是 PersistedModel。

模型没有远程访问方法。

您需要将您的 common/models/starship.json 更改为

{
     "name": "Starship",
     "plural": "starships",
     "base": "PersistedModel",
     "idInjection": true,
     "options": {
         "validateUpsert": true
     },
     "properties":  {},
     "validations":  [],
     "relations": {},
     "acls": [],
     "methods": {}
}

查看http://apidocs.strongloop.com/loopback/#persistedmodel了解更多信息。

【讨论】:

  • 这是正确的!我没想到要检查 Model 和 PersistedModel 的文档。但如果我考虑过,我代理的 api 是数据库支持的,因此是 RESTful CRUD api。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多