问题描述
我的服务
public submitBooking(createBooking: CreateBooking) {
const body = this.getSaveBookingRequestBody(createBooking);
//const json = JSON.Stringify(body) //It throws 415 error
return this.httpClient.post(this.baseUrl + 'Save', body )
.subscribe();
}
private getSaveBookingRequestBody(createBooking: CreateBooking): any {
const body = {
'Booking': {
'Header': this.getBookingHeader(createBooking), //It works fine.
'Items': [this.getBookingItems(createBooking)],
},
'TestOnly': !environment.production,
};
this.Logger.log(body);
return body;
}
private getBookingItems(createBooking: CreateBooking): any {
const bookingItem = {
'CountryOfOrigin': createBooking.booking.countryoforigin,
'VehicleValue': createBooking.booking.valueofvechicle,
'SerialNumber': createBooking.booking.bookingNumber,
'Description': createBooking.booking.description,
'Mobility': createBooking.booking.mobility,
'Currency': createBooking.booking.currency,
'Weight': createBooking.booking.weight,
'Year': createBooking.booking.year,
'Cbm': createBooking.booking.cbm,
//This is an array it doesn't work.
'SubUnits':[
createBooking.booking.relatedVehicles.forEach(element => {
const units = {
'RelationType': element.relation,
'Weight': element.weight,
'Year': element.year,
};
})],
};
return bookingItem;
我的角度模型和WEB-API对象不同
我也尝试过JSON.Stringify(unitsArray)
并将其返回给SubUnits
I have also tried the JSON.Stringify(unitsArray)
and returning it to SubUnits
const unitsArray = [];
createBooking.booking.relatedVehicles.forEach(element => {
const units = {
'RelationType': element.relation,
'SerialNumber': element.chassisno,
'Weight': element.weight,
'Year': element.year,
};
unitsArray.push(units);
});
SubUnits : JSON.stringify(unitsArray); // It also doesn't work with API.
版本:角度5打字稿2.4.2
Version :Angular 5Typescript 2.4.2
推荐答案
const bookingItem = {
...
'SubUnits': [
createBooking.booking.relatedVehicles.forEach(element => {
const units = {
'RelationType': element.relation,
'Weight': element.weight,
'Year': element.year,
};
})
]
...
};
此循环不会填充bookingItem.SubUnits
中的数组. Array.forEach
不返回值.除此之外,从未使用变量units
.您可以做的是使用Array.map
创建一个新数组.
This loop doesnt fill the array in bookingItem.SubUnits
. Array.forEach
does not return a value. Besides that, the variable units
is never used. What you can do instead is create a new array using Array.map
.
'SubUnits': [
createBooking.booking.relatedVehicles.map(element => ({
'RelationType': element.relation,
'Weight': element.weight,
'Year': element.year
}))
]
这将使一个数组包含一个来自createBooking.booking.relatedVechiles
的数组元素.我不确定这是否是您要的东西,但是它在您的OP中.
This makes an array with 1 array element from createBooking.booking.relatedVechiles
. I am not sure if that's what you are going for, but it is in your OP.
这篇关于角度http发布发送复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!