API route is fetching data, and I have implemented pagination to fetch post data.
On page 2 of pagination, I want to append the fetched data to the existing array.
Function utilized for data fetching
const posts = ref([]);
const page = ref(1);
const getPosts = async (page) => {
await axios
.get("/api/explore/gallery/?page=" + page)
.then((response) => {
if (page === 1) {
posts.value = response.data;
} else {
posts.value = { ...posts.value, ...response.data };
}
});
};
From page 2 onwards, the fetched data will be added to the existing array.
Outcome with posts.value = { ...posts.value, ...response.data };
https://i.sstatic.net/gfZKv.png
The id
now starts from 51
instead of 1 - 100
.
I also attempted
posts.value = [ ...posts.value, ...response.data ];
but encountered
PostApi.js:12 Uncaught (in promise) TypeError: Invalid attempt to spread non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
at _nonIterableSpread (PostApi.js:12:39)
at _toConsumableArray (PostApi.js:10:131)
at eval (PostApi.js?c0c4:15:21)
response.data
has this structure
https://i.sstatic.net/1Ysrh.png
post.value
appears like this