Invoking a Vuex action inside another Vuex action

After transitioning to the Vuex store, I've been steadily moving forward. However, my current hurdle involves invoking an action from within another action in Vuex. My grasp on Vue.js is still a work in progress.

Current versions

  1. Vue 3
  2. Vuex 4
  3. If necessary, I utilize Electron Vue.js dev tools

/src/store/index.js - Existing code

/* eslint-disable no-redeclare */

import { createStore } from 'vuex'
import axios from 'axios'

export default createStore({
  state: {
    ...
  },
  mutations: {
    setQuery(state, query) {
      // refers this.state.query
      state.query = query
    },

    setOnlinePresenceInitialPageLoad(state, presence) {
      // refers this.state.online & this.state.initialPageLoad
      state.online = presence
      state.initialPageLoad = false
    },

    setRequestErrorAndStatus(state, { _status, code }) {
      // refers this.state.request.error & this.state.request._status
      state.request.error = _status
      state.request._status = code
    },

    setResults(state, processed) {
      state.request.results = processed
    }
  },
  actions: {
    callApi({ commit, state, dispatch }) {
      axios.get(state.axios.targetURL, {
        baseURL: state.axios.config.baseURL,
        params: {
          days: state.axios.config.params.days,
          q: state.query,
          key: state.axios.config.params.key,
        },
      }).then(response => {
        console.log(response.status)
        commit('setOnlinePresenceInitialPageLoad', true);
        dispatch('formatResults', response.data);
      }).catch(error => {
        if (error.response) {
          console.log(error.response.status)
        } else if (error.request) {
          console.log(error.request.status)
        } else {
          console.log("Couldn't find the problem")
        }
      })
    },
    formatResults(context, payload) {
      const processedData = {
          ...
      }
      console.log(processedData);
      context.commit('setResults', processData);
    }
  },
  modules: {
  }
})

In the example above, callApi() triggers the formatResults() in the successful segment of the Promise.

Current state (Web browser)

https://i.sstatic.net/SxMHz.jpg

Within the code, I attempted to output the variable processedData. I assumed it would be displayed in the console.

Current state (Vue Devtools)

https://i.sstatic.net/OIOHO.jpg

I'm also curious as to why formatResults() seems to never complete.

If async functions can resolve this issue, then I am interested in learning the necessary steps. Any advice is greatly appreciated.

Thank you for your assistance!

Answer №1

Based on the limited information provided, I will make an educated guess...

After observing that console.log(response.status) and

console.log("Couldn't find the problem")
were activated, along with formatResults (as shown in the vuex screenshot), it appears that the issue lies within the formatResults function.

formatResults(context, payload) {
  const processedData = {
      // potential error area
  }
  console.log(processedData);
  context.commit('setResults', processData);
}

When an error occurs, it will be caught by the catch block

if (error.response) {
  console.log(error.response.status)
} else if (error.request) {
  console.log(error.request.status)
} else {
  // Log and handle the error here...
  console.log("Couldn't find the problem")
}

Consider using console.error(error) to pinpoint the root cause of the error

if (error.response) {
  console.log(error.response.status)
} else if (error.request) {
  console.log(error.request.status)
} else {
  console.error(error)
}

This should provide you with enough insights to troubleshoot the underlying issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the reordering of items in an Angular JS array causing a malfunction with the first item

It appears that there may be a syntax error present in my code. Within my controller, I have a set of 4 functions: adding a new item, deleting the current item, moving an item backward (up) in the array, and moving an item forward (down) in the array. Al ...

What is the method for identifying which input field the user has chosen on a web page retrieved from a server?

I attempted the code below without achieving the desired outcome. Any assistance would be greatly appreciated. UIPasteboard *pb = [UIPasteboard generalPasteboard]; [pb setString:passwordName]; NSString *jScriptString; jScriptString = [NSString string ...

Exploring the functionality of utilizing dual loops with v-for in Vue.js

I am facing an issue with my table code where I need to repeat it 6 times to match the day column above. I would like to use v-for and make use of a variable called "count" which represents the number of days. Can someone assist me with this task? <tabl ...

Vuetify - custom filter for advanced datatable restrictions

Currently, I am faced with a challenge in filtering data from a table where the object names are similar and case sensitive, such as "A", "Aa", or "a". I am struggling to filter the data by these exact values when using a v-select bound to the search funct ...

The error message popping up reads as follows: "TypeError: _this2.setState cannot

I am encountering an error that I don't quite understand. How can I resolve this issue and why is it occurring in the first place? Although I am receiving the correct response from the API, I am also getting an error immediately after making a call. ...

What is the CoffeeScript alternative for () => { return test() }?

Currently, I am attempting to write this code in CoffeeScript and finding myself at a standstill... this.helpers({ events: () => { return Events.find({}); } }); ...

Is it possible to apply capitalization or convert the value of props to uppercase in React?

Despite attempting the toUpperCase method, I am unable to capitalize my props value. Here's the code I have: export default function Navbar(props) { return ( <> <div> <nav class="navbar navbar-expand-lg bg-b ...

Creating a sort button in HTML that can efficiently sort various divs within a table is a useful tool for enhancing user experience

My HTML table is populated with various <td> elements. How can I arrange these divs based on IMDb rating, TomatoMeter, etc... [ CSS code is not provided below ] <table> <tr class="row"> <td class="column"> <br> ...

What is the best way to set up a session using jQuery?

I've been troubleshooting my code and I can't seem to figure out why the jquery.session.js file isn't working. Can someone help me find a solution? $.session.set('rmng_time', remaining_seconds); alert("j session "+$.sessi ...

Using a JavaScript variable to be displayed in a PHP code

Can someone please help me troubleshoot this code? I am attempting to display a JavaScript variable in PHP after applying a regex, but I keep getting the error Uncaught TypeError: document.getElementById(...).html is not a function $.post('display.ph ...

Looking for guidance on integrating Forms with VueX and script setup? Are you utilizing v-model in your implementation?

UPDATE: I'm contemplating whether or not to abandon VueX entirely due to its outdated status, with Pinia being the preferred choice nowadays. Can someone confirm this? https://stackblitz.com/edit/vue-script-setup-with-vuex-hmrk5d?file=src/store.ts ...

Ways to display "No records" message when the filter in the material table in Angular returns no results

How can I implement a "No Records Message" for when the current table is displaying empty data? Check out this link for examples of material tables in AngularJS: https://material.angular.io/components/table/examples ...

Exploring the potential of jQuery and AJAX for dynamic content generation:

I have developed a jQuery plugin that pulls data from a JSON feed (specifically YouTube) and then presents the results in a DIV. Everything is working well until I need to display the results with different configurations, such as more videos or from anoth ...

Ensure that the central section of the slider remains illuminated

Looking for help with customizing my "product" slider, lightSlider. I want the middle div to always be highlighted when navigating through the slider. Here's what it looks like currently: Link to screenshot - current. My goal is to achieve this look: ...

What is the best way to deliver HTML content to an ASP.NET MVC JSON function?

Here is my jQuery code that I have written along with the json function called InsertMatlabJson. However, I am facing an issue where no text is being passed to the .NET json function. function insert() { var url = '<%=Url.Content( ...

Showing changes in state in real-time using ReactJS: A quick guide

Hello, I am currently facing an issue while trying to add a comment and display it immediately on the DOM. Whenever I type any word in the form box, it does not appear in the box. Additionally, pressing the enter key does not trigger a POST request. Could ...

Moving the input box down to the lower portion of the screen

My goal is to create an interactive input box that glides smoothly to the bottom of the screen when clicked by the user. However, the current implementation causes the input box to move too far down, requiring the user to scroll down in order to see it. H ...

When initially compiling Angular 5, an error (TS2339) may occur, but after a successful compilation, everything runs smoothly

In a unique scenario, I wrote code that fetches information from an API server without knowing the structure of the response fields. Once I receive the response, I need to create a form for updating the data and sending it back. To handle unknown ngModel p ...

"Encountering a problem with posting API requests using Express

I've encountered a problem where I keep receiving a 404 error when trying to post to a specific route. Here's the code snippet: app.js (code snippet for app.js) .... module.exports = app; api.js (code snippet for api.js) .... module.export ...

Customizing Column Background Color with AngularJS

https://i.sstatic.net/OHFFF.png My current webapp highlights the day of the week on a table for the current day and the day two weeks from now. However, I'm facing an issue with adjusting the highlight if either of these days falls on a holiday. Curr ...