0

enter image description here

I try override find api of strongloop rest endpoint. I want to return an array of objects. But how do I specify the schema for the object? As you can see from the picture above, the model schema is empty.

Below is the code of my company model remoteMethod:

    Company.remoteMethod(
        'find',
        {
            accepts: {arg: 'msg', type: 'string'},
            returns: {type: 'array', root: true},
            http: {path: '/', verb:'get'}
        }
    )
Shaohua Huang
  • 698
  • 5
  • 19
  • Did you create a `company` model? If so, what is the configuration? That config defines your schema... Look at the [documentation for how to create models](https://docs.strongloop.com/display/public/LB/Defining+models). – Jordan Kasper Jan 25 '16 at 13:36
  • @jakerella: Yes, I create a company model. But I want to overwrite the api /companies {GET} by the remote method and it is already able to be achieved. My problem is that, in the api explorer, the Model Schema is [{}], which is not showing the properties of the object. – Shaohua Huang Jan 25 '16 at 13:55
  • Aaah, I see... I'm not sure how to do that. – Jordan Kasper Jan 25 '16 at 14:05

1 Answers1

3

If I understand you right, your'e trying to show at this section the returned model as follows:

[
  {
    "companyProperty1": "companyProperty1Type",
    "companyProperty2": "companyProperty2Type",
    .
    .
    "companyPropertyN": "companyPropertyNType",
  }
]

In order to achieve this kind of return type representation, you need to define your return type in remoteMethod options to be an array of the desired model.

Here is your code, with the required edit, using modelName propery of Model base class:

Company.remoteMethod(
    'find',
    {
        accepts: {arg: 'msg', type: 'string'},
        returns: {type: [Company.modelName], root: true},
        http: {path: '/', verb:'get'}
    }
)
Reuven Chacha
  • 879
  • 8
  • 20
  • What does Company.modelName refer to? Can you give an example? Thx – Shaohua Huang Jan 25 '16 at 14:16
  • Edited answer, added reference. Model.modelName returns the model name of the caller model. in your case is simple and you can just write 'Company', but when using mixins for example, it has to be more general. – Reuven Chacha Jan 25 '16 at 14:33
  • Thanks very much. Now it can show the schema. Another question is, how do I customise this schema? I mean if my original model contains {propsA.propsB}, however I only need propsA. How am I able to achieve it? – Shaohua Huang Jan 28 '16 at 10:57
  • I'm not sure I understood your question, but if you're asking how to hide some of the properties of the model, see my answer here: http://stackoverflow.com/questions/35039816/how-can-i-hide-the-id-attribute-in-loopback-explorer/35042171#35042171 it might answer your question – Reuven Chacha Jan 28 '16 at 11:02
  • Sorry, I have one more question. What if I find propsA is not really a nice name, I want to display it as propsA1. Is it possible? – Shaohua Huang Jan 28 '16 at 11:07