Wednesday 29 November 2023

Nuxt : n2 is not a function

I'm using Nuxt 3 and I have an TypeError like this :

Uncaught TypeError: n2 is not a function

I have a button calling my function toggleSelectRow with a @click.prevent

And here is the function :

const toggleSelectRow = (keyword) => {
    if (keywordsSelected.value.find((kw) => kw.uid_kw === keyword.uid_kw) && props.keywords.length > 1) {
        keywordsSelected.value = []
        nuxtApp.$bus.$emit('filter-from-list', '');
    } else {
        keywordsSelected.value = [keyword]
        nuxtApp.$bus.$emit('filter-from-list', keyword.uid_kw);
  }
}

Here is the receiver part :

$bus.$on('filter-from-list', (newKeyword) => {   filters.keyword = (props.keywords.find((kw) => kw.uid_kw === newKeyword)) ? newKeyword : null   if (!filters.keyword) {
    refreshNuxtData('average-keyword-position');
    return;   }

  fetchKeywordPosition({ keyword: newKeyword }); });

nuxtApp is defined above with : const nuxtApp = useNuxtApp();

I think my problem come from the emit but I don't know why because if I refresh the page, it will work.

Fixing test 1

I tried to use a declared function in the receiver component like this :

function filterFromList(newKeyword){
  filters.keyword = (props.keywords.find((kw) => kw.uid_kw === newKeyword)) ? newKeyword : null
  if (!filters.keyword) {
    refreshNuxtData('average-keyword-position');
    return;
  }

  fetchKeywordPosition({ keyword: newKeyword });
}

to call it like this : $bus.$on('filter-from-list', filterFromList); but I still have the same error so I think the problem must come from the sender/receiver.



from Nuxt : n2 is not a function

No comments:

Post a Comment