It seems like you're encountering a server error when calling getBanners() from within the updateBanners() function after a condition is met. A 500 error typically indicates a server-side issue, so the problem might not be with your Vue.js code but rather with your Laravel backend.
However, I'll provide some guidance on how to handle the async functions in Vue 3 with the Composition API, and how to remove a banner from the banners ref array by its ID.
First, ensure that you are correctly awaiting the getBanners() function call within the updateBanners() function. If getBanners() is an async function, you should await it to ensure that the operation completes before proceeding.
Here's how you might modify your updateBanners() function:
async function updateBanners() {
console.log('updating Banner ...');
try {
const response = await axios.post("https://domain.client.fi/api/update", updateBannerData);
if (response.data.banner == null) {
console.log(response.data.banner);
console.log('updating Banners array object ...');
await getBanners(); // Make sure to await this async call
}
} catch (error) {
console.error("Error updating banners:", error);
}
}
To remove a banner from the banners ref array by its ID, you can use the Array.prototype.filter() method. Here's a function that does that:
function removeBannerById(bannerId) {
banners.value = banners.value.filter(banner => banner.id !== bannerId);
}
You would call this function with the ID of the banner you want to remove.
Regarding your question about whether it would be easier to update the banners ref and call the entire banner array again, it depends on the specific requirements of your application. If the list of banners is not too large and you want to ensure that the client has the most up-to-date list, then fetching the entire list again might be a simple and effective approach. However, if you're only updating or removing a single banner, it might be more efficient to update the banners ref array directly without making an additional network request.
Remember to check your Laravel logs for the specific error causing the 500 response. It could be an issue with the route, controller method, database query, or other server-side logic. Fixing the server-side error should resolve the 500 error you're encountering.