Search code examples
loopbackjsstrongloop

how to get specific field in an included object


I'm trying to get a specific field in a api call from an included object.

I get an empty array.

filter =

{"where":{"type":"person"}, "include":["objectA"], "fields":"objectA.name"}

What am i doing wrong?


Solution

  • If for example you have the following data model:

    Model: Customer.
    Fields: id, name.
    
    Model: Order.
    Fields: id, date, description, customerId.
    
    Order.belongsTo(Customer, {foreignKey: ‘customerId’});
    

    You can get only the Customer name by writing this filter:

    var filter = {
      "where": {
        "id": 1
      },
      "include": [
        {
          "relation": "customer",
          "scope": {
            "fields": [
              "name"
            ]
          }
        }
      ]
    }
    
    Order.find(filter, function(err, order) {
        ...
    }) 
    

    and in your case i'm guessing the filter suppose to be something like this:

    {
      "where": {
        "type": "person"
      },
      "include": {
        "relation": "objectA",
        "scope": {
            "fields": ["objectA.name"]
        }
      }
    }