发布到两个表环回



我在环回时遇到问题,我想尝试 POST 到另一个表。 但不能。 这里是我想发布到环回资源管理器的代码 在 http://localhost:1337/explorer/#!/Order/Order_create

{
"Code": "ASDASDASDE",
"DP": 0,
"Discount": 0,
"DiscountNominal": 0,
"TotalPrice": 0,
"RequestDate": "string",
"DeliveryDate": "2017-06-22T07:14:18.815Z",
"TotalQty": 0,
"Destination": "string",
"Phone": "string",
"Status": "string",
"PaymentType": "string",
"DealerCode": "D912839",
"Active": true,
"Deleted": true,
"CreatedBy": "string",
"CreatedDate": "2017-06-22T07:14:18.815Z",
"CreateAgent": "string",
"UpdatedBy": "string",
"UpdatedDate": "2017-06-22T07:14:18.815Z",
"UpdateAgent": "string",
"KioskCode": "A1234",
"orderDetails":[{
"Code": "ASD123",
"IsRetur": true,
"Price": 0,
"IMEI": "string",
"Status": "string",
"OrderCode": "ASDASDASDE",
"ProductCode": "SM-1923",
"Active": true,
"Deleted": true,
"CreatedBy": "string",
"CreatedDate": "2017-06-22T07:14:19.045Z",
"CreateAgent": "string",
"UpdatedBy": "string",
"UpdatedDate": "2017-06-22T07:14:19.045Z",
"UpdateAgent": "string"
}]
}

假设,我想 POST 到表 订单,并在 JSON 的单引号中表订单详细信息。 已插入订单表,但无法插入订单明细表。

这是我的订单.JSON

{
"name": "Order",
"plural": "Order",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"mixins": {
"TimeStamp": {}
},
"properties": {
"Code": {
"type": "string",
"id": true,
"required": true
....
},
"validations": [],
"relations": {
"Dealer": {
"type": "belongsTo",
"model": "Dealer",
"foreignKey": "DealerCode"
},
"orderDetails": {
"type": "hasMany",
"model": "OrderDetail",
"foreignKey": "OrderCode"
},
"Kiosk": {
"type": "belongsTo",
"model": "Kiosk",
"foreignKey": "KioskCode"
}
},
"acls": [],
"methods": {}
}

这是我的订单详细信息.JSON

{
"name": "OrderDetail",
"plural": "orderDetails",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"mixins": {
"TimeStamp": {}
},
"properties": {
"Code": {
"type": "string",
"id": true,
"required": true
}
.....
},
"validations": [],
"relations": {
"Order": {
"type": "belongsTo",
"model": "Order",
"foreignKey": "OrderCode"
},
"Product": {
"type": "belongsTo",
"model": "Product",
"foreignKey": "ProductCode"
}
},
"acls": [],
"methods": {}
}

订单具有 primaryKey: Code 和 orderDetails 有一个外键:引用 order: Code 的 OrderCode。 什么解决方案??感谢您的帮助

您可以按照以下步骤操作: 1. 在订单.js文件中定义一个远程方法:

Order.remoteMethod('saveNewOrder', {
accepts: {arg: 'orderInstance', type: 'json'},
http: {path:'/order-create', verb:'post'},
returns: {arg: 'result', type: 'json'}
});

2. 使用 "orderInstance" 参数定义 saveNewOrder 函数:

Order.saveNewOrder = function(orderInstance, cb){
var order = new Order(orderInstance["order"])
order.save().then(function(savedOrder,err){
if(err)
throw err
else
savedOrder.orderDetails.create(orderInstance["orderDetails"], function(err, createdOrderDetails){
if(err)
throw err
else
console.log("insertion done:",createdOrderDetails)
})
})
}
  1. 更改 JSON 以匹配以下格式:

    {
    "orderInstance":{
    "order":{
    "Code": "ASDASDASDE",
    "DP": 0,
    "Discount": 0,
    ...
    },
    "orderDetails":[{
    "Code": "ASD123",
    "IsRetur": true,
    "Price": 0,
    ...
    }]
    }
    }
    
  2. 将 JSON 发布到 http://localhost:3000/api/orders/order-create

相关内容

  • 没有找到相关文章

最新更新