Loopbackjs 在Strongloop环回中创建多个

Loopbackjs 在Strongloop环回中创建多个,loopbackjs,strongloop,Loopbackjs,Strongloop,我有一个订单模型,它有许多OrderItem模型。但是,一旦客户想要创建订单,它必须首先创建订单对象,然后对于他添加到购物篮中的每个产品,他需要分别创建响应的OrderItems。正如您可能注意到的,它会导致许多冗余请求。也许我可以为使用产品列表的OrderItems创建一个自定义方法。但我想知道是否有一个类似createMany的内置机制,因为这是一个非常有用的操作 订单模型 { "name": "Order", "plural": "Orders", "base": "Pers

我有一个订单模型,它有许多OrderItem模型。但是,一旦客户想要创建订单,它必须首先创建订单对象,然后对于他添加到购物篮中的每个产品,他需要分别创建响应的OrderItems。正如您可能注意到的,它会导致许多冗余请求。也许我可以为使用产品列表的OrderItems创建一个自定义方法。但我想知道是否有一个类似createMany的内置机制,因为这是一个非常有用的操作

订单模型

 {
  "name": "Order",
  "plural": "Orders",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "customerId": {
      "type": "number",
      "required": true
    },
    "branchId": {
      "type": "number",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "orderItems": {
      "type": "hasMany",
      "model": "OrderItem",
      "foreignKey": "orderId"
    }
  },
  "acls": [],
  "methods": []
}
订单项模型

{
  "name": "OrderItem",
  "plural": "OrderItems",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "UnitPrice": {
      "type": "number"
    },
    "productId": {
      "type": "number",
      "required": true
    },
    "purchaseOrderId": {
      "type": "number",
      "required": true
    },
    "quantity": {
      "type": "number"
    }
  },
  "validations": [],
  "relations": {
    "product": {
      "type": "belongsTo",
      "model": "Product",
      "foreignKey": "productId"
    },
    "purchaseOrder": {
      "type": "belongsTo",
      "model": "PurchaseOrder",
      "foreignKey": ""
    }

  },
  "acls": [],
  "methods": []
}

Loopback“create”方法还接受一个对象数组(请参阅文档),因此您应该尝试创建一个“create”调用并发送一个OrderItems数组。

很漂亮,没有注意到PersistedModel有数组输入,感谢您的指点。