DarkLite1
DarkLite1

Reputation: 14735

Function not found in module

Suppost you have a module file like this:

authService.js

import { Screen } from 'quasar'
import * as authPopup from 'src/services/auth/authPopup'
import * as authRedirect from 'src/services/auth/authRedirect'

const loginMethod = Screen.lt.sm ? 'redirect' : 'popup'

export const auth = (loginMethod === 'popup')
  ? { loginMethod, authPopup }
  : { loginMethod, authRedirect }

If would be great if it could be consumed like this:

consumer.js

import { auth } from 'src/services/auth/authService'

const getProfile = () => {
  if (!auth.getAccount()) { return Promise.reject(new Error('no logged on user')) }

  console.log('loginMethod ', auth.loginMethod)
}

But it errors out saying that auth.getAccount() is not a function. Why is that? We're clearly exporting the auth with all functions and the string in the authService.js file.

Thank you for your help.

Upvotes: 0

Views: 63

Answers (1)

Bergi
Bergi

Reputation: 664936

I suppose you meant to do

export const auth = (loginMethod === 'popup')
  ? { loginMethod, ...authPopup }
  : { loginMethod, ...authRedirect };

so that the exported auth object actually will contain all the methods from the respective imported namespace object, not a .authPopup or .authRedirect property. Or alternatively, more explicitly, you can write:

export const auth = { loginMethod: Screen.lt.sm ? 'redirect' : 'popup' };
Object.assign(auth, auth.loginMethod === 'popup' ? authPopup : authRedirect);

Upvotes: 1

Related Questions