2016-03-14 39 views
0

我已经设置了一个简单的“产品”模型(即{id:“string”,“name”:string等)),并使用REST连接器设置一个数据源到一个远程URL,返回一个包含数十个字段,我该如何将远程响应中的字段映射到本地模型?每当我执行我的方法时,我都会收回来自远程的原始响应....我期望至少能够取回我的模型的空白版本。Loopback REST连接器,对模型的数据映射响应?

enter image description here

回答

1

我敢肯定你将不得不重写你的模型的find()方法和手动执行此映射工作。

事情是这样的:

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) 
     }); 
    } 
}