vuejs / Discussion

Vue.js discussion
167 stars 17 forks source link

vuejs can not find method or variable inside a callback function #1324

Open rasimavci opened 6 years ago

rasimavci commented 6 years ago

Hi, I can not find answer to problem in anywhere, its no possible to work with callback functions. I am using an API and it has event registration functions which takes callback.

In code below there is event register function for the API which takes a callback, neither exported vue method or parameter works in the callback. if I can call a method for it to mutate the state, I think I do not a variable for that et all.

Here is the all steps;

1- first API should be created using imported API 2- should login 3- after login verified (after few seconds) register to an event using myAPI.on with a callback function

so I call addEventListeners for step 3, but the problem is its not working…

This is the problematic code

    addEventListeners(todo) {
         myAPI.on('contacts:changed', function (params) {
            console.log('contacs changed') 
heyyo = params
addNotes(params)
       });
    }

Here is the all code for the vue file

<template>
  <div id="toolbar">
    <i @click="Login()" class="glyphicon glyphicon-book"></i>
    <i @click="refreshContacts(params)" class="glyphicon glyphicon-time"></i>
    <i @click="addEventListeners('trying')" class="glyphicon glyphicon-download-alt"></i>
        <i @click="addNotes(heyyo)" class="glyphicon glyphicon-plus"></i>
    <i @click="toggleFavorite"
      class="glyphicon glyphicon-star"
      :class="{starred: activeNote.favorite}"></i>
    <i @click="notlistesi = !notlistesi" class="glyphicon glyphicon-remove"></i>
  </div>
</template>

<script>
import { addNote, addNotes, deleteNote, toggleFavorite } from '../vuex/actions'
import createAPI from '../api.js'
var heyyo
export default {
  data () {
    return {
      heyyo: ''
    }
  },
methods: {
    Login() {
var credentials = {
    username: 'rastek',
    password: '1234'
  }
  console.log('connect method runs for ' + credentials.username)
  myAPI.connect(credentials)
    },

    refreshContacts(todo) {
        myAPI.user.refreshContacts()
    },
    addEventListeners(todo) {
         myAPI.on('contacts:changed', function (params) {
            console.log('contacs changed') 
heyyo = params
addNotes(params)

       });
    }

},
  vuex: {
    getters: {
      activeNote: state => state.activeNote
    },
    actions: {
      addNote,
      addNotes,
      deleteNote,
      toggleFavorite
    }
  }
}

 var myAPI = createAPI({
    authentication: {
      subscription: {
        expires: 3600,
        service: ['database', 'message'],
        protocol: 'https',
      },
    },
    logs: {
      logLevel: 'debug',
      enableFcsLogs: true
    },

  })

  createAPI()

</script>
LinusBorg commented 6 years ago

This whole discussion repository was depracated like, two years ago.

Please use forum.vuejs.org

rasimavci commented 6 years ago

Hi, I already asked that question on forum but couldnt get an answer, anyway I fixed it, thanks.