Struggling with sending an object to a function in Vuex

I've been trying to send an object to a Vuex action, but for some reason it isn't working as expected.

The purpose of this action is to take an object, iterate through its properties, and generate a string that will be used to filter results in an API request.

Here is my component setup:

  data() {
    return {
      usersData: [],
      modals: {
        removeUser: false,
        id: "",
        filterSearch: false
      },
      filters: {
        role: "user",
        active: "true",
        search: "",
        pageSize: "",
        page: ""
      }
    };
  },
  mounted() {
    this.getUsers();
  },
  methods: {
    getUsers() {
      console.log("Object before passing = " + this.filters);
      var params = this.$store.dispatch('getQueryString', this.filters);
    }
  }

store.js

import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);
Vue.use(axios);

export default new Vuex.Store({
  state: { },
  mutations: { },
  actions: {
    getQueryString(payload) {
      console.log("Object passed = " + payload);
      let queryString = "";
      let counter = 0;
      for (var key in payload) {
        if (payload[key]) {
          let connector = (counter > 0) ? "&" : "?";
          queryString += connector + key + "=" + payload[key];
          counter++;
        }
      }
      return queryString;
    }
  },

When I check the console, it displays:

Object before passing = [object Object]
, indicating an issue with how the object is being handled before reaching the action. While Vue treats the object like an object within the component, it seems to print as a string during the initial step.

Is there anyone who knows how to successfully pass the object to this action?

Answer №1

Try using

console.log("object passed = ", payload)
instead of
console.log("object passed = " + payload)
. By doing this, you will be able to see your object. Access your object inside the payload by using payload.filters

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

Transform JSON data into a new object

I'm currently exploring methods to manipulate my JSON data and create a separate object with the modified information. Specifically, I am working with this JSON file: My goal is to transform [dataset][data] into the following format: [Date.UTC(2013 ...

AngularMistake Stream

Having some trouble with importing the Observable class and interval method. Need assistance getting this set up properly here ...

Issue with Braintree drop-in form: Nonce string generation problem

I've encountered a peculiar issue while trying to utilize the dropin form from Braintree. Successfully integrating the form into a view, here's how it looks: <form id="createTransactionForm" method="post" action="#&qu ...

Highlight or unhighlight text using Javascript

Currently, I am facing a challenge in highlighting specific words on an HTML page. Although I have succeeded in highlighting the desired element, I am struggling with unhighlighting the previous word when a new search is conducted. Despite my attempts to i ...

What is the best way to extract multiple return values from the $.post function in jQuery?

How can I separate two variables retrieved from the server using the $.post method? Currently, when I attempt this, I receive a combined mixed value. Below is the code snippet: $(".spot_me").click(function() { var idea = $(this).attr("id"); ...

What is causing the object, which is clearly defined (as shown in the callback to JSONLoader), to be interpreted as undefined

Why is the THREE.Mesh() object showing as undefined even though it was defined in a THREE.JSONLoader()? Here is the code snippet... 1: var player; 2: var playerCallback = function(geo, mats){ 3: player = new THREE.Mesh(geo, new THREE.MeshFaceMaterial( ...

A guide on converting a JSON array into CSV format while customizing attributes

I'm currently developing a Node.js application using JavaScript. I have an array that I need to convert into a CSV format with specific attributes. var data = [ { "Name":"nom1", "Division":"first", "Cities":['city1','c ...

The $.each function seems to be stuck and not cycling through the

Dealing with a rather intricate JSON structure, I'm encountering difficulty iterating through it using the $.each() function. It seems to be related to the unusual 2-dimensional array passed in the value section of the standard array (hopefully that m ...

employing requirejs for fetching npm modules

I need help running this code in HTML. Can someone show me how to use require.js for this? const monerojs = require("monero-javascript"); I tried the following but it doesn't work, I keep getting a "Script error for "monero-javascript"". requirejs([ ...

How can I dynamically update the sidebar in Ionic 3 post-login without the need to reload the app or refresh the browser?

I have successfully implemented login and sign up functionality in my ionic 3 app. However, I am facing an issue where the username is not updating in the sidebar instantly after logging in. Currently, I need to refresh the browser or close and reopen the ...

How can one go about constructing abstract models using Prisma ORM?

Among my various prisma models, there are common fields such as audit fields like created_at and updated_at. model User { id Int @id @default(autoincrement()) created_at DateTime @default(now()) updated_at DateTime @updatedAt email ...

Setting up Express JS version 4 without using the app.configure method

Express js version 4.0 has been recently released and I have encountered an issue with my Express 3-app after updating because the app.configure() method was removed in the new version. This is how my Express 3 configuration looks like: // all environmen ...

Tips for utilizing vue-class-component

<script lang="ts"> import { defineComponent, ref, nextTick, unref, onMounted } from 'vue'; import { useScript } from '/@/hooks/web/useScript'; const BAI_DU_MAP_URL = 'https://api.map.baidu.com/getscript?v=3.0& ...

The Vue-router is updating the URL without triggering a re-render of the component

After confirming that history mode is enabled, I initialize vue-router in the following way: const router = new Router({ mode: 'history', base: process.env.BASE_URL, routes: routes, }); At present, the current route is /page/item/8, and I ...

Implementing JavaScript to visually showcase an external content on my website

Just made my first post! I was wondering about a javascript issue...here's the code I'm working with: <html> <head> <title>Geolocation Demo</title> </head> <body> <h1>Geolocation Demo</ ...

How to incorporate a JavaScript class into a VueJS project

Looking to incorporate a JavaScript class into my Vue application. The class structure is as follows: class className { constructor() { ... } function1() { ... } static funtion2() { ... } } Attemptin ...

Generating hidden form fields dynamically with AngularJS

In the midst of developing an AngularJS application, I'm faced with a requirement to pass hidden variables to a third-party application. These variables need to be fetched from a database. To accomplish this, I've implemented the following code ...

Discovering nested nodes in JSON by utilizing multiple string values

My screen requires price calculations based on two parameters: primary (activity) and children (ticketType). Previously, this process was handled in a hacky way. I decided that storing prices in a JSON object generated at runtime and updating them based o ...

What is the process for placing a component on the right side of the sidebar?

Here is the code snippet I am working with: <template> <div class="main"> <sidebar /> <router-view /> </div> </template> The sidebar has a width of 260px. I need to ensure that any comp ...

numerous yields within a calculated attribute

I am facing an issue with setting up multiple returns in a computed property. I am unsure why it is not functioning as expected. I also attempted to repurpose the arrow function, but without success. My objective here is to trigger the 'nearby' m ...