我需要执行发送数组的请求。但是当我这样做时,我得到 null
结果。
结果请求负载:
[
{nom_name: "Test", cost: 500, quantity: 1, parentOrder_id: 19},
{nom_name: "35634", cost: 100, quantity: 1, parentOrder_id: 19}
]
结果预览:
{
address: null
apartment: null
code: null
cost: null
date_delivery: null
floor: null
hierarchyLevel: 1
is_new: true
name: null
nom_name: null
order_id: 40
parentOrder_id: null
payment: null
quantity: null
telephone: null
}
如何修复以防止将 null
值发送到我要发送的值的服务器?这样 post 请求就发送一个数组,而不是单个对象。
服务:
create(order: Order[]): Observable<Order> {
return this.http.post<Order>(`${env.url}/api/orders/create`, order);
}
ts:
onSave() {
const order: Order[] =
this.order.list.map(e => {
const { order_id, ...rest } = e
return ({ ...rest, parentOrder_id: 19 })
})
this._order.create(order)
.subscribe(
() => {
console.log('Data Saved.')
},
error => {
console.log(error)
}
);
}
在服务器上我使用 Nodejs 和 ORM Sequelize。
node.js Controller :
module.exports.create = function (req, res) {
const order = db.orders.bulkCreate([
{
nom_name: req.body.nom_name,
quantity: req.body.quantity,
cost: req.body.cost,
parentOrder_id: req.body.parentOrder_id
}
])
res.status(201).json(order);
};
最佳答案
我个人并没有对sequelize进行太多工作,但我很确定您需要直接传递request-body对象,因为req.body
包含数据数组,因此例如,req.body.nom_name
将是未定义
。另外,bulkCreate
被定义为 async-function在发送响应之前您应该等待:
module.exports.create = async (req, res) => {
const orders = await db.orders.bulkCreate(req.body);
res.status(201).json(orders);
};
关于node.js - 如何发出将数组发送到服务器的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64100840/