How to execute a function in store.js from a different JS file within a Vue project

I am currently utilizing Vuex to store my data in a file called store.js. Within this setup, I have a mutation method named 'autoUpdateDb' as seen below:

store.js :

import Vuex from "vuex";
import Vue from "vue";
import { serv } from "./serviceMixin";

Vue.use(Vuex);

export default new Vuex.Store({

mutations: {
    autoUpdateDb(state, payload) {
        functionA();
    },        
},

actions: {}

});

However, when attempting to invoke the "autoUpdateDb" function, an error stating "(void 0) is undefined" is encountered. The actual function being called, 'functionA()', resides in another JavaScript file named serviceMixin.js

serviceMixin.js :

 functionA(){
      console.log("Hello");
            }

Could you kindly guide me on the correct way to call the required function?

Many thanks!

Answer №1

Consider the following scenario where we have two files named First.js and Second.js residing in the same folder. Let's take a look at the code snippet from the file First.js:

export default function FunctionOne(){
    console.log("Executing code from First.js")
}

Now, let's move on to the contents of Second.js:

import FunctionOne from './First.js'
FunctionOne()

Answer №2

If your project utilizes a module bundler such as webpack, make sure to include the function in your store.js file.

const {utilityFunction} = require('./serviceMixin');

Next, remember to export utilityFunction:

function utilityFunction(){

}

module.exports = {
    utilityFunction
}

Answer №3

If you have a file named serviceMixin.js, make sure to correctly define the function by including the keyword function and exporting it like this:

 export function functionA(){
      console.log("Hello");
            }

Next, in your store file, import the function at the top and call it within an action like so:

import {functionA} from './serviceMixin'
... 
mutations: {
      autoUpdateDb(state, payload) {
       functionA();
    },

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

Removing Redux State in ReactDeleting data from Redux in a

After displaying a list of data on a table, I have implemented a View component where specific data from the list is passed. However, I am facing an issue when attempting to delete something in the View modal dialog as the redux data delete reducer does no ...

transform nested object into a flat object using JavaScript

here is the given JSON format: - { "_id": "5c1c4b2defb4ab11f801f30d", "name": "Ray15", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afddced69e9aefc8c2cec6c381ccc0c2">[email protected]</a>" ...

Update the src attribute of img elements using regular expressions

Figured it out in case anyone might find it useful, here's the solution: var feed = feeds.entries[i].content; var parsedFeed = feed.replace(/src=/gi, "tempsrc="); var tmpHolder = document.createElement('div'); tmpHolder.innerH ...

Guide on converting JSON object to condensed rows when using ng-repeat

From the database, I am receiving a JSON object which is one large object: $scope.totalsum = { "A1": 1155000, "A2": null, "A3": 2133, "A31": 29292, "A32": 2321, "A33": 232342, ...

Load media upon the click of an image

I'm currently in the process of designing a team page for our website. Each team member's photo is displayed in a grid layout, with two boxes positioned below containing various content. Box 1: This box consists of basic text information. Box 2: ...

The object _plugins_vuetify__WEBPACK_IMPORTED_MODULE_8__.default does not contain a constructor function

https://i.sstatic.net/Cqx5B.pngAfter attempting to add Vuetify to my project, I encountered an issue despite trying different solutions from various sources. Below is the code snippet from my vuetify.js file: import Vue from 'vue' import Vuetify ...

Ensuring Map Safety in Typescript

Imagine having a Map structure like the one found in file CategoryMap.ts export default new Map<number, SubCategory[]>([ [11, [100, 101]], [12, [102, 103]], ... ]) Is there a way to create a type guard for this Map? import categoryMap fro ...

npm: Generating debug and production builds while ensuring accurate dependency management

I am in the process of developing a single page application using TypeScript along with various other dependencies such as jQuery, immutable, lodash, and React. Each resulting module is integrated into the project using requirejs. My goal is to generate t ...

Bootstrap Carousel with descriptions in a separate container without any display or hiding transitions

I have created a slider using Bootstrap and some JavaScript code that I modified from various sources. Everything is working well, but I want to remove the transition or animation that occurs when the caption changes. Currently, the captions seem to slide ...

Is the webdriver.io waituntil method designed to return a boolean value of true or false

I am working on an automation framework using webdriver.io v5. I need to receive a boolean response from the code snippet below: waitAndCheckForContactToBePresent(contactName) { return browser.waitUntil((value) => { return this.chec ...

What is the best way to update a specific element within a web page?

My current implementation involves utilizing this code snippet within a functional component : return ( <div style={{ height: "700px", overflow: "auto" }}> <InfiniteScroll pageStart={0} loadMore={Fet ...

What is preventing me from executing node.js and socket.io within a screen or utilizing Forever?

I'm encountering an issue with running nodejs in a screen. The problem arises when I leave the screen and no sockets are connected. The next person trying to connect will face an error message until the screen is reopened using screen -R node. Once th ...

Is it possible to access JSON with a numeric key and receive undefined as a result?

I've been attempting to extract information from my JSON data, but I keep getting an undefined result. Here is a snippet of my JSON: { "1": "A", "2": "B", "3": "C", "4": "D", "5": "E", "6": "F", "key":"pair" } This i ...

There was an issue with the vue-server-renderer where the render function or template was not defined in the component

In my project with Vue 2.7 and webpack4 for SSR, I encountered an error message stating: render function or template not defined in component: anonymous. This issue arises under the following circumstances: When using a child component. <template> ...

Incorporating the angular UI router effectively by reusing the same templateUrl and controller multiple times

Exploring the AngularUI Router framework for the first time, I am curious about how to enhance the code snippet below. Everything is functioning well at the moment, but as the project progresses, it will feature 20 questions or more. I want to avoid repea ...

Trouble with useEffect not triggering in NextJS 13.4 (app router) application

When trying to fetch data from the next API route, I encountered an issue where the useEffect method did not trigger on page reload. Additionally, I was unable to make this component async as Next.js does not allow async functions in client components. pa ...

Obtain data in JSON format through an xmlhttp request

I originally used jQuery for this task, but I now want to switch to regular JavaScript as I'll be incorporating it into phonegap. I aim to avoid relying on different JS frameworks every time I make a server request, which could potentially improve per ...

Is there a way to make the switch case condition shorter?

I need to dynamically select a text input field based on the variable type chosen, without having to include the textfield component in every single condition. const chooseInputType = () => { switch (type) { case 'boolean': ...

Adjusting the size of all elements on a webpage

Upon completing my project, I noticed that my localhost:3000 is zoomed in at 125%, causing it to appear less than ideal at 100% zoom. Is there a way to adjust the zoom/scale of my website to match how it appeared on my localhost environment? I came across ...

Utilizing PHP to create an interactive website

As a novice PHP developer, I took to Google in search of tutorials on creating dynamic PHP websites. What I found was that many tutorials used the $_GET variable to manipulate URLs and make them appear like this: example.com/?page=home example.com/?page= ...