I am trying to access my store ($store) variable by importing it from the file
import store from "../store"
but it I am getting this error while console.log(store)
:
Uncaught ReferenceError: store is not defined
at eval (eval at _callee$ (functions.services.js:1:1), <anonymous>:1:1)
at _callee$ (functions.services.js?5215:46:1)
at tryCatch (runtime.js?96cf:63:1)
at Generator.invoke [as _invoke] (runtime.js?96cf:294:1)
at Generator.eval [as next] (runtime.js?96cf:119:1)
at asyncGeneratorStep (asyncToGenerator.js?1da1:3:1)
at _next (asyncToGenerator.js?1da1:25:1)
at eval (asyncToGenerator.js?1da1:32:1)
at new Promise (<anonymous>)
at eval (asyncToGenerator.js?1da1:21:1)
eval @ VM388:1
_callee$ @ functions.services.js?5215:46
tryCatch @ runtime.js?96cf:63
invoke @ runtime.js?96cf:294
eval @ runtime.js?96cf:119
asyncGeneratorStep @ asyncToGenerator.js?1da1:3
_next @ asyncToGenerator.js?1da1:25
eval @ asyncToGenerator.js?1da1:32
eval @ asyncToGenerator.js?1da1:21
eval @ functions.services.js?5215:42
fetchF @ functions.services.js?5215:42
login @ SignIn.vue?c790:92
eval @ SignIn.vue?c790:49
eval @ runtime-dom.esm-bundler.js?830f:1457
callWithErrorHandling @ runtime-core.esm-bundler.js?5c40:6737
callWithAsyncErrorHandling @ runtime-core.esm-bundler.js?5c40:6746
invoker @ runtime-dom.esm-bundler.js?830f:357
Here is the file where I am using it: “functions.services.js”
import store from "@/store"
export function fetchF(link, body, method, isTokenNeeded = true, isJSON = true) {
return new Promise(async (resolve, reject) => {
var access_token = getLocalStorage("access_token");
var refresh_token = getLocalStorage("refresh_token");
if (!(access_token == null && refresh_token == null) || isTokenNeeded == false) {
fetch(store.serverLink + link, {
method,
headers: {
authorization: `Bearer ${access_token} ${refresh_token}`,
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}).then((response) => response[isJSON == true ? "json" : "text"]()).then(rsp => {
setLocalStorage("access_token", rsp.access_token);
setLocalStorage("refresh_token", rsp.refresh_token);
resolve(rsp);
}).catch(error => {
setLocalStorage("access_token", "");
setLocalStorage("refresh_token", "");
store.dispatch("changeData", {
option: "alertEl",
value: "Please log in, and try again",
});
return resolve({ isSuccess: false, err: "Please log in, and try again" });
})
} else {
if (!(access_token == null && refresh_token == null) && isTokenNeeded == true) {
store.dispatch("changeData", {
option: "alertEl",
value: "Please log in, and try again",
});
}
return resolve({ isSuccess: false, err: "Please log in, and try again" });
}
});
}
here is the store file “index.js”:
import { createStore } from 'vuex'
var isDev = true
export const store = createStore({
state: {
serverLink: isDev ? "http://localhost:3000" : "http://.......:3000",
isAuth: false,
isInitiated: false,
alertEl: ""
},
mutations: {
},
getters: {
getAuthStatus: state => {
return state.isAuth
},
getInitiatedStatus: state => {
return state.isInitiated
}
},
setters: {
},
actions: {
},
modules: {
}
})
I tried to use it in router.js too, for authenticating routes before entering them but the same issue happens