Ryan Rife
Ryan Rife

Reputation: 169

Loopback REST connector, data mapping response to model?

I've setup a simple "product" model (ie {id:"string","name":string, etc}) and setup a datasource using the REST connector to a remote URL that returns a JSON blob containing dozens of fields, how do I go about mapping the fields from the remote response to my local model? Whenever I execute my method I'm getting back the raw response from the remote....I was expecting, at a minimum, to get back an empty version of my model.

enter image description here

Upvotes: 0

Views: 410

Answers (1)

Aleks
Aleks

Reputation: 5854

I'm pretty sure you will have to override the find() method on your model and perform this mapping work manually.

Something like this:

module.exports = function(app) {
  var Product = app.models.Product;
  var find = Product.find;

  Product.find = function(filter, cb) {
      // invoke the default method
      find.call(Product, function(err, original_results) {
          var results = {};     // a placeholder for your expected results

          results.name = original_results.id;
          results.name = original_results.name;
          results.description = original_results.long_description;
          // and so on    

          cb(null, results)
      });
  }
}

Upvotes: 1

Related Questions