2015-12-04 149 views
0

當我運行以下代碼時,「卡」節點無法作爲嵌套實體加載到「源」字段中。我回顧了可以從'nested.property'映射的源代碼,它應該在項目目標中創建該對象和屬性,但它似乎不適合我。任何幫助將非常感激。Node.js automapper-ts包不映射嵌套的對象/屬性

var jsf = require('json-schema-faker'); 
var automapper = require('automapper-ts').automapper; 

var schema = { 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "name": "customer", 
    "title": "customer", 
    "description": "Canonical Customer", 
    "type": "object", 
    "properties": { 
     "email": { 
      "type": "string", 
      "format": "email" 
     }, 
     "description": { 
      "type": "string" 
     }, 
     "phone": { 
      "type": "string" 
     }, 
     "card": { 
      "type": "object", 
      "properties": { 
       "name": { 
        "type": "string" 
       }, 
       "expiryMonth": { 
        "type": "string", 
        "minLength": 2, 
        "maxLength": 2, 
        "pattern": "^(1[0-2])|([0][1-9])$" 
       }, 
       "expiryYear": { 
        "type": "string", 
        "minLength": 4, 
        "maxLength": 4, 
        "pattern": "^20([1-9][5-9])$" 
       }, 
       "number": { 
        "type": "string", 
        "pattern": "^(?:4[0-9]{12}(?:[0-9]{3})?|5[12345][0-9]{14}|3[47][0-9]{13}|3(?:0[]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35[0-9]{3})[0-9]{11})$" 
       }, 
       "cvc": { 
        "type": "integer", 
        "maximum": 9999 
       }, 
       "zip": { 
        "type": "string" 
       } 
      }, 
      "required": [ 
       "name", 
       "expiryMonth", 
       "expiryYear", 
       "number", 
       "zip" 
      ], 
      "additionalProperties": false 
     }, 
     "trialEnd": { 
      "type": "number", 
      "minimum": 1448420692, 
      "maximum": 4098384000 
     } 
    }, 
    "required": [ 
     "email", 
     "card" 
    ], 
    "additionalProperties": false 
}; 

automapper.createMap('a', 'b') 
    .forMember('description', function(opts) {opts.mapFrom('description');}) 
    .forMember('email', function(opts) { opts.mapFrom('email');}) 
    .forMember('source.name', function(opts) { opts.mapFrom('card.name');}) 
    .forMember('source.exp_month', function(opts){ opts.mapFrom('card.expiryMonth');}) 
    .forMember('source.exp_year', function(opts) { opts.mapFrom('card.expiryYear');}) 
    .forMember('source.number', function(opts){ opts.mapFrom('card.number');}) 
    .forMember('source.address_zip', function(opts) { opts.mapFrom('card.zip');}) 
    .forMember('source.cvc', function(opts) { opts.mapFrom('card.cvc');}) 
    .forMember('source.object', function(){return 'card'}) 
    .forMember('trial_end', function(opts) { opts.mapFrom('trialEnd')}) 

var cn = jsf(schema); 

var cn2= automapper.map('a', 'b', cn); 

回答

1

哪個版本的AutoMapperTS當前有用?版本1.6.1應無縫支持您正在嘗試實施的操作。

請注意最近庫名已被更改,您應該安裝automapper-ts NPM軟件包。舊包裹被標記爲過時。它是相同的包,因此你使用它的代碼應該是完全正確的;)。順便說一下,這裏是NPM網址:https://www.npmjs.com/package/automapper-ts

乾杯,伯特