Search code examples
node.jsloopbackjsstrongloop

Return additional fields from related model in StrongLoop


In a situation similar to this one, Getting joined data from strongloop/loopback, where one has Products and product Categories, how does one return the Category Name rather than the id (foreign key) as the default response for /Products? I've been able to hide the id field but not return the name. Thanks.


Solution

  • Supposing you have the relation Product hasOne Category, called productCat

    With Node API

    Product.find({
     include: {
        relation: 'productCat', // include the Category object
        scope: { // further filter the Category object
          fields: 'name', // only show category name
        }
      }
    }, function(err, results) { /* ... */});
    

    With REST API

    GET api/Products?filter={"include":{"relation":"productCat","scope":{"fields":"name"}}}
    

    Hope this helps (haven't tested it but it should work)