[Vuejs]-Submitting a post that belongs to current user – how to define the relation on client-side in Vue?

0👍

I just managed to get it working correctly, I’m not sure why it suddenly started working as I am sure I had tried this before, but I solved it by doing the following within my component’s script element:

<script>
import PostService from '@/services/PostService'

export default {
    data () {
        return {
            post: {
                title: null,
                body: null,
                UserId: null
            }
        }
    },
    methods: {
        async submit () {
            this.post.UserId = this.$store.state.user.id

            try {
                await PostService.post(this.post)
            } catch (err) {
                console.log(err.response.data.error)
            }
        }
    }
}
</script>

Posts now create and display as normal. If anyone knows anything I’m technicially not doing right in any of my approach though please let me know, as I am still learning Node, Express and Vue.

Leave a comment