hi,
Am tyring to cancel the WIX store orders via Http request but its show below error Can help out to fix the error
Error:
1. Update Data: {"_id":"28a6f3ac-c4a9-42fe-b6f3-4e6f622c65e2","fulfillmentStatus":"CANCELED"}
2. Item [] does not exist in collection [collection].
3. Error: WDE0073: Item [] does not exist in collection [collection].
Code:
export function post_cancelOrder(request) {
console.log("Received request to cancel order");
return request.body.json()
.then(async (body) => {
let { orderId } = body;
if (!orderId) {
console.error("Missing orderId in request");
return { status: 'error', message: 'Missing orderId' };
}
console.log(`Searching for order with number: ${orderId}`);
try {
// Query to check if the order exists
const result = await wixData.query("Stores/Orders")
.eq("number", orderId.toString()) // Ensure orderId is a string
.find({ suppressAuth: true });
console.log("Query result:", result);
if (result.items.length === 0) {
console.error(`Order with number ${orderId} not found`);
return { status: 'error', message: `Order with number ${orderId} not found` };
}
const order = result.items[0];
const orderIdString = order._id;
console.log(`Found order. _id: ${orderIdString}, Updating status to CANCELLED`);
// Log order fields for debugging
console.log("Order fields:", order);
// Ensure _id is a string (for safety)
const _idString = String(orderIdString);
console.log("_id passed for update:", _idString);
// Check if the _id is valid
if (!_idString) {
console.error("Invalid _id, unable to update:", order);
return { status: 'error', message: "Invalid order _id" };
}
// Check all orders in collection for reference
const allOrders = await wixData.query("Stores/Orders").limit(10).find({ suppressAuth: true });
console.log("All Orders in Collection:", allOrders.items.map(o => o._id));
// **Check if the order is present before updating**
const orderToUpdate = allOrders.items.find(order => order._id === _idString);
if (!orderToUpdate) {
console.error("Order to update not found in the collection.");
return { status: 'error', message: "Order to update not found" };
}
// Prepare the update data
const updateData = {
_id: _idString, // Ensure _id is passed as a string
fulfillmentStatus: "CANCELED"
};
console.log("Update Data:", updateData); // Log update data to verify it's correct
// Perform the update operation
const updateResult = await wixData.update("Stores/Orders", updateData, { suppressAuth: true });
console.log("Update result:", updateResult);
return { status: 'success', message: 'Order cancelled successfully' };
} catch (error) {
console.error("Error:", error);
return { status: 'error', message: error.message };
}
})
.catch(err => {
console.error("Error parsing request body:", err);
return { status: 'error', message: "Invalid request format" };
});
}
top of page
To test this feature, visit your live site.
Wix Store Order Cancellation
Wix Store Order Cancellation
1 comment
Comments (1)
bottom of page
Hi,
When getting an error message the first thing I recommend doing is making sure you understand it fully. You can use this reference to check the meaning of error codes: https://dev.wix.com/docs/velo/apis/wix-data/error-codes
WDE0073: Item {item ID} does not exist in collection {collection name}. Check that an item with the ID you're requesting exists in the collection you're querying.
Next look for the last log that you got: 1. Update Data: {"_id":"28a6f3ac-c4a9-42fe-b6f3-4e6f622c65e2","fulfillmentStatus":"CANCELED"}
That means that somewhere right after this you are requesting an item that doesn't exist using it's ID. Most likely it is the update operation below:
Hopefully that's enough to get you back on track! Best, Eitan