[Vuejs]-Vue.js 2 axios computed value returning "invalid date"

0👍

I don’t think moment is the problem here, because if I change the
function’s return to simply this.posts.date or this.date, I get an
error of undefined. So somehow, I am not accessing that property, and
I can’t figure out why.

Indeed… Your problem is not even related to Vue or Axios, but to Vanilla JS.

The AJAX response gives you an array of objects similar to this:

let posts = [
  { date: '1486231256' },
  { date: '1502199613' }
];

To get the dates, you cannot simply do posts.date because posts is an array! As suggested by @abhishekkannojia, you have to loop through the array:

let posts = [
  { date: '1486231256' },
  { date: '1502199613' }
];

for (const post of posts) {
  console.log(moment.unix(post.date).format('YYYY-MM-DD'));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Leave a comment