Mike Barwick Mike Barwick - 4 months ago 22
Javascript Question

Vue.js & Moment.js Output Date Rending Object Instead of Date String

Not sure where I'm going wrong here, but I'm trying to output current date in a Vue.js component. But instead of returning a date string (i.e. 2017/02/02) it's returning a string of the object (??). Lost...

<template>
<div>
<input type="text" :value="initialDate">
</div>
</template>

<script>

export default {
props: ['date', 'user'],

computed: {
initialDate() {
return this.date ? this.date : this.fetchCurrentDate
}
},

methods: {
fetchCurrentDate() {
return window.moment()
},
}
}
</script>


In the browser, I'm seeing this as the input value:

function boundFn(a) { var l = arguments.length; return l ? l > 1 ? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx) }


When it should be an actual date string.

Answer Source

You need to call the fetchCurrentDate function. Not reference it.

initialDate() {
      return this.date ? this.date : this.fetchCurrentDate()
}