Post 到两个表环回
Post to two Tables Loopback
我有环回问题,我想尝试 POST 到另一个 table。但它不能。这里是我想要 POST 到 Loopback Explorer 的代码
在 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到table订单,table订单详情在JSON的单引号中。已插入订单 Table,但无法插入订单详细信息 table。
这是我的 Order.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": {}
}
这是我的 orderDetail.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 有一个外键: OrderCode 引用Order: Code。什么解决办法??感谢您的帮助
您可以按照以下步骤操作:
1. 在你的 order.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)
})
})
}
更改您的 json 以匹配此格式:
{
"orderInstance":{
"order":{
"Code": "ASDASDASDE",
"DP": 0,
"Discount": 0,
...
},
"orderDetails":[{
"Code": "ASD123",
"IsRetur": true,
"Price": 0,
...
}]
}
}
post 你的 json 到 http://localhost:3000/api/orders/order-create
我有环回问题,我想尝试 POST 到另一个 table。但它不能。这里是我想要 POST 到 Loopback Explorer 的代码 在 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到table订单,table订单详情在JSON的单引号中。已插入订单 Table,但无法插入订单详细信息 table。
这是我的 Order.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": {}
}
这是我的 orderDetail.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 有一个外键: OrderCode 引用Order: Code。什么解决办法??感谢您的帮助
您可以按照以下步骤操作: 1. 在你的 order.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)
})
})
}
更改您的 json 以匹配此格式:
{ "orderInstance":{ "order":{ "Code": "ASDASDASDE", "DP": 0, "Discount": 0, ... }, "orderDetails":[{ "Code": "ASD123", "IsRetur": true, "Price": 0, ... }] } }
post 你的 json 到 http://localhost:3000/api/orders/order-create