【问题标题】:How can I work with Nested Objects in Loopback 4 using the cli b4 model command如何使用 cli b4 model 命令在 Loopback 4 中使用嵌套对象
【发布时间】:2019-10-18 19:53:22
【问题描述】:

我正在处理需要为使用 lb4 建模的深层嵌套对象。我有机会获得有关此示例 JSON 代码的帮助吗?

{ "cardAcceptor": { "address": { "city": "Foster City", "country": "RU", "county": "San Mateo", "state": "CA", "zipCode": "94404" }, "idCode": "ABCD1234ABCD123", "name": "ABCD", "terminalId": "ABCD1234" }, "destinationCurrencyCode": "840", "markUpRate": "1", "retrievalReferenceNumber": "201010101031", "sourceAmount": "100", "sourceCurrencyCode": "643", "systemsTraceAuditNumber": "350421" }

【问题讨论】:

    标签: loopbackjs strongloop loopback4


    【解决方案1】:

    您可以为嵌套对象创建单独的模型,

    以上述数据结构为例

    你可以有类似的东西

    export class CardAcceptor extends Address {
    
      @property({
        type: 'string',
        required: true,
      })
      address: string;
    
      @property({
        type: 'string',
        required: true,
      })
      idCode: string
    
      @property({
        type: 'string',
        required: true,
      })
      name: string;
    
      @property({
        type: 'string',
        required: true,
      })
      terminalId: string;
    
      constructor(data?: Partial<CardAcceptor>) {
        super(data);
      }
    }
    
    export class Address extends Entity {
    
      @property({
        type: 'string',
        required: true,
      })
      city: string;
    
      @property({
        type: 'string',
        required: true,
      })
      country: string
    
      @property({
        type: 'string',
        required: true,
      })
      county: string;
    
      @property({
        type: 'string',
        required: true,
      })
      state: string;
    
    
      @property({
        type: 'boolean',
        required: true,
      })
      zipCode: boolean;
    
      constructor(data?: Partial<Address>) {
        super(data);
      }
    }
    
    
    export class BaseModel extends CardAcceptor {
    
    
      @property.array(CardAcceptor)
      cardAcceptor: CardAcceptor;
    
      @property({
        type: 'string',
        required: true,
      })
      destinationCurrencyCode: string;
    
      @property({
        type: 'string',
        required: true,
      })
      markUpRate: string
    
      @property({
        type: 'string',
        required: true,
      })
      retrievalReferenceNumber: string;
    
      @property({
        type: 'string',
        required: true,
      })
      sourceAmount: string;
    
    
      @property({
        type: 'boolean',
        required: true,
      })
      sourceCurrencyCode: boolean;
    
      @property({
        type: 'array',
        itemType: 'string',
        default: [],
      })
      systemsTraceAuditNumber?: string[];
    
    
      constructor(data?: Partial<Game>) {
        super(data);
      }
    }
    
    

    希望这会有所帮助。 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      相关资源
      最近更新 更多