we are facing issue with fetch “api” on wixStores_onOrderPaid(event).
In the scenario the order is placed and we are receiving 201(created) response at back-end, but along with that we are facing some errors like,
- Unhandled rejection Error: body used already for: API_key …
- Unhandled rejection Error: body used already for: API_key …
- “jsonPayload”: {
“message”:“[”[object Promise]“]”
}
Please look into the below attached code and guide us how to proceed with the further steps, it will be very helpfull.
export async function wixStores_onOrderPaid(event) {
let today = new Date();
var deliveryDates = new Date();
deliveryDates.setDate(today.getDate()+ 5 );
const paidOrderId = event._id;
event.lineItems.forEach(item => {
item.SKUNumber = item.sku;
})
let DeliveryCompanyName = event.billingInfo.company;
let DeliveryAddress1 = event.billingInfo.address.addressLine.substring( 0 , 49 );
let DeliveryAddress2 = event.billingInfo.address.addressLine.substring( 0 , 49 );
let DeliveryCity = event.billingInfo.address.city;
let DeliveryCounty = event.billingInfo.address.subdivision;
let DeliveryPostCode = event.billingInfo.address.postalCode;
let DeliveryCountry = event.billingInfo.address.country;
let DeliveryPhone = event.billingInfo.phone;
let OutboundRef = “before i expect” ;
let ReleaseDate = today;
let DeliveryDate = deliveryDates;
let DeliveryTime = event.shippingInfo.estimatedDeliveryTime;
let Haulier = “boxtrax” ;
let BranchID = “HSQ” ;
let CustomerID = event.number;
let Weight = event.totals.weight;
Weight = Math.ceil(Weight);
let CustRef = event.billingInfo.firstName + " " + event.billingInfo.lastName;
let Remarks = “Notify confirmed delivery details asap!”
let PickItems = event.lineItems;
let NOP = event.totals.quantity;
let payload = {
“DeliveryCompanyName” : DeliveryCompanyName,
“DeliveryAddress1” : DeliveryAddress1,
“DeliveryAddress2” : DeliveryAddress2,
“DeliveryCity” : DeliveryCity,
“DeliveryCounty” : DeliveryCounty,
“DeliveryPostCode” : DeliveryPostCode,
“DeliveryCountry” : DeliveryCountry,
“DeliveryPhone” : DeliveryPhone,
“DeliveryContact” : CustRef,
“OutboundRef” : OutboundRef,
“ReleaseDate” : ReleaseDate,
“DeliveryDate” : DeliveryDate,
“DeliveryTime” : DeliveryTime,
“Haulier” : Haulier,
“PickItems” : PickItems,
“BranchID” : BranchID,
“CustomerID” : CustomerID,
“NOP” : NOP,
“Weight” : Weight,
“Cube” : 1 ,
“CustRef” : CustRef,
“Remarks” : Remarks
}
let headers = {
‘Content-type’ : ‘application/json’ ,
‘boxtopkey’ : ‘3536335F6F6772614320726941206C656E7572428’
};
console.log( 'body string = ' , JSON.stringify(payload));
let options = {
method: ‘POST’ ,
headers: headers,
body: JSON.stringify(payload)
};
await fetch( ‘API_key…’ , options)
.then((httpResponse) => {
console.log( 'After Fetch' + httpResponse.json());
**let** catch = httpResponse.text();
setTimeout( **function** () {
console.log( "Result After 13 Seconds" +catch);
}, 13000 );
})
. **catch** ((error) => {
console.log( 'Oops... ' + error+ "ErrorEnd :(" );
});
}