0

After a new project is created, I'd like to route the user to another page so they can add more information to the project.

This is working:

createProject() {
      ProjectService.createProject(this.project)
        .then(response => {
          this.$router.push({
            name: "project-update",
            params: { id: response.data.data.id }
          });
        })
}

I'd like to use vuex to handle all this, but this is not working.

createProject() {
  this.$store
    .dispatch("project/postProject", this.project)
    .then(response => {
      this.$router.push({
        name: "project-update",
        params: { id: response.data.data.id }
      });
    })
    .catch(() => {});
}

The error I'm getting is: "state.projects.push is not a function"

This is my postProject action in Vuex:

  postProject({ commit, dispatch }, project) {
    return ProjectService.createProject(project)
      .then(() => {
        commit('ADD_PROJECT', project);
        const notification = {
          type: 'success',
          message: 'Your project has been created!'
        };
        dispatch('notification/add', notification, { root: true });
      })
      .catch(error => {
        const notification = {
          type: 'error',
          message: 'There was a problem creating your project: ' + error.message
        };
        dispatch('notification/add', notification, { root: true });
        throw error;
      });
  }

Looks like the context of 'this' is not reaching the router or the push function therein. How can I access the router and route to that next page?

6
  • Can you add your code of dispatched action and mutation. Commented Feb 10, 2020 at 6:24
  • The action and dispatch are working fine, the data is successfully stored in the database and state, only the router is not working. Commented Feb 10, 2020 at 6:29
  • you need to include the router module into that vuex module and use router.push() Commented Feb 10, 2020 at 6:30
  • then how do you get this error state.projects.push is not a function? Just try commenting router.push line and check it its still throwing this error. Commented Feb 10, 2020 at 6:31
  • @KarmaBlackshaw How do I add the router module? Should I simply place what I have here into the action, or do I have to include something? Commented Feb 10, 2020 at 6:36

2 Answers 2

1

What you can do is import your router module into your vuex module like so:

import {router} from "../main.js"
// or
import router from '../router'

export default {
  actions: {
    createProject () {
      this.$store
        .dispatch("project/postProject", this.project)
        .then(response => {
          router.push({
            name: "project-update",
            params: { id: response.data.data.id }
          })
        })
        .catch(() => { })
    }
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

can you show your new createProject method ? if you still used this.$router, kindly changed it into just router only
I deleted my latest comment once I noticed that. Changed my code to reflect yours and I'm still getting the "state.projects.push is not a function" error
Tried both import variations
the router part is done. Next thing is your postProject which seems to have a wrong implementation. Can you show your postProject method ?
Most probably your state variable projects is an object and not an array. You should check that first by doing console of typeOf of your variable state.projects.
|
0

I have a same issue but I solved by doing this:

this.$router.replace("/");

Having issue in vuex and nuxt store by using this : this.$router.push("/");

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.