1

I'm creating a system comment with Laravel 5.5, Vue 2 and Vuex. I Can't post a comment. I get in my console, two errors:

TypeError: this.addComment is not a function

Error: Request failed with status code 422

This is my code:

import { addComment } from '../../store/actions'
export default {
  computed: {
    addComment
  },
  vuex: {
    actions: { addComment }
  },
  data () {...},
  methods: {
    sendComment: function () {
        this.addComment({
            commentable_id: this.id,
            commentable_type: this.model,
            content: this.content,
            reply: this.reply
        })
    }

actions.js code

export const addComment = function ({dispatch}, comment) {
    return axios.post('/comments', comment).then((response) => {
        dispatch('ADD_COMMENT', response.data)
    })
};

All my routes, controller and mutations are tested and work well.

2 Answers 2

1

You don't need to import actions into your components as long as the store is registered globally. So you simply need to call addComment like this:

this.$store.dispatch('addComment', {
  commentable_id: this.id,
  commentable_type: this.model,
  content: this.content,
  reply: this.reply
})

Also, putting addComment in computed doesn't make sense so you have to remove it.

In your addComment action, I believe it's called commit not dispatch:

export const addComment = function ({commit}, comment) {
    return axios.post('/comments', comment).then((response) => {
        commit('ADD_COMMENT', response.data)
    })
}
Sign up to request clarification or add additional context in comments.

4 Comments

I develop a lot more in php. I am obliged here to do javascript, really painful. I made the corrections and I get this now ERROR: [vuex] unknown action type: addComment AND TypeError: this.$store.dispatch(...) is undefined
It's ok, can you add the content of your main Vue component and vuex store to the question?
Ok cool, you need to import the whole store object and add it to your root Vue instance in order to make this.$store available.
In my Form.vue, I added this code: import store from '../../store/Store' but error is the same
0

My Store.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export const state = {
    comments: []
};

export const mutations = {
    ADD_COMMENTS (state, comments) {
        state.comments.push(...comments)
    },
    ADD_COMMENT (state, comment) {
        if (comment.reply) {
            let c = state.comments.find((c) => c.id === comment.reply);
            if (c.replies === undefined) {
                c.replies = []
            }
            c.replies.push(comment)
        } else {
            state.comments.push(comment)
        },
    DELETE_COMMENT () {...}
};

let store = new Vuex.Store({
    state: state,
    mutations: mutations
});
export default store;

My Form.vue Component:

import { addComment } from '../../store/actions'
import { mapActions } from 'vuex'

export default {
  vuex: {
    actions: { addComment }
  },
  data () {
    return {
      content: '',
      loading: false
    }
  },
  props: {
    id: Number,
    model: String,
    reply: {
      type: Number,
      default: 0
    }
  },
  methods: {
    sendComment: function () {
        this.loading = true;
        this.$store.dispatch('addComment', {
          commentable_id: this.id,
          commentable_type: this.model,
          content: this.content,
          reply: this.reply
        }).catch((error) => {
            this.error = error.response.data
        }).then(() => {
            this.loading = false
        })
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.