Eric Eric - 1 month ago 10
reST (reStructuredText) Question

CRUD methods and endpoints for RESTful api not being generated in proxy api server

I'm trying to proxy a server (http://www.swapi.co/api/starships for practice, then a Salesforce api for production). This will be the mobile backend for a React Native app. I'm following the documentation here: http://loopback.io/doc/en/lb2/REST-connector.html#resource-operations. However, when using the generators to create a 'starship' datasource that uses CRUD operations and a Starship model, nothing shows up when I try to explore the api using the built-in explorer. When proxying a RESTful API, I'd like to expose it using a RESTful API, is this even available yet with Loopback?

Here is the screenshot of the explorer I'm using to view the api:

Loopback api explorer screenshot

Here are the steps I'm taking:

$ 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


However, when I navigate to the explorer, only the Users api shows up, nothing for Starships. Any ideas on what I could be doing wrong? Here are the contents of the generated files that I could find:

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": {}
}


server/datasources.json

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


server/model-config.json

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

Answer

The problem is, you're using Model as base instead of PersistedModel.

Model doesn't have remote methods to access.

You need change your common/models/starship.json to

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

Check http://apidocs.strongloop.com/loopback/#persistedmodel for more information.