I have some pagination on an Api route which was used to fetch the post's data.
When the pagination is on page 2, I would like to add the fetched data from the page and add to the existing array.
Function used to fetch the data
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 };
}
});
};
So when the page is 2 onward, the fetched data will add to the existing array.
The result if I used posts.value = { ...posts.value, ...response.data };
The id start from 51 instead of 1 - 100.
I have also tried posts.value = [ ...posts.value, ...response.data ]; but returned
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 look like this
post.value look like this



[ ... ]not{ ... }Also, you need to make sure you're spreading the actual array, not something else. Why are you referring toposts.valueispostsis an array? There's maybe aposts[0].valuebut an array does not have a.valueposts.value = posts.value.concat(response.data)response.data.dataand the existing array ispost.value.data, which means you needpost.value.data = [ ...post.value.data, reponse.data.data ]refcan't take an array as it's a mutable ref object, which has a single property value that points to the inner value. Try instantiate the array withconst post = {value: []}orconst post = []