Skip to content
This repository has been archived by the owner on Apr 19, 2019. It is now read-only.

Promise not working #68

Closed
lgertel opened this issue May 10, 2017 · 13 comments
Closed

Promise not working #68

lgertel opened this issue May 10, 2017 · 13 comments
Labels

Comments

@lgertel
Copy link

lgertel commented May 10, 2017

I'm doing something #wrong.
I have this code on my store, but when retrieving from my component the then in dispatch is called before the callback.

On my component:

methods: {
      fetchData() {
        this.loading = true
        this.$store.dispatch('setApplicationsRef').then(response => {
          console.log('data dne')
          this.loading = false
        })
      }
    }
And on my module:

const actions = {
  setApplicationsRef: firebaseAction(({commit, bindFirebaseRef, state}) => {
    return new Promise((resolve, reject) => {
      bindFirebaseRef('applications', applicationsRef, {
        readyCallback: () => {
          resolve()
        }
      })
    })
  })
}

The console.log('data dne') is getting called before the resolve on actions

@posva
Copy link
Owner

posva commented May 10, 2017

Can you provide a repro using this: https://jsfiddle.net/posva/6w3ks04x/ ?

@lgertel
Copy link
Author

lgertel commented May 10, 2017

I have two components:

Applications.vue

<template>
  <div>
    <nav class="nav">
    </nav>

    <router-view></router-view>
  </div>
</template>
<script>
  import { mapGetters } from 'vuex'

  import Topbar from './Topbar.vue'

  export default {
    components: {
      appTopbar: Topbar
    },
    computed: {
      ...mapGetters([
        'applications'
      ])
    },
    data () {
      return {
        loading: true
      }
    },
    methods: {
      fetchData() {
        this.loading = true
        this.$store.dispatch('setApplicationsRef').then(response => {
          console.log(response)
          this.loading = false
        })
      }
    },
    watch: {
      '$route' (to, from) {
        this.fetchData()
      }
    },
    created() {
      this.fetchData()
    }
  }
</script>
<style scoped>
  .nav {
    -webkit-box-align: stretch;
    -ms-flex-align: stretch;
    align-items: stretch;
    background-color: #f1f3f7;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    height: 3.25rem;
    position: relative;
    text-align: center;
    z-index: 10;

    border-top: 1px solid #dce1eb;
    border-bottom: 1px solid #dce1eb;
  }
</style>

and applications.js

import firebase from 'firebase'

import { firebaseMutations } from 'vuexfire'
import { firebaseAction } from 'vuexfire'

//removed the config

const firebaseApp = firebase.initializeApp(config)
const db = firebaseApp.database()
const applicationsRef = db.ref('applications')

const mutations = {
  ...firebaseMutations
}

const state = {
  applications: [],
  application: null
}

const getters = {
  applications: state => {
    return state.applications
  },
  countApplications: state => {
    return state.applications.length
  },
  application: state => {
    return state.application
  }
}

const actions = {
  setApplicationsRef: firebaseAction(({commit, bindFirebaseRef, state}) => {
    return new Promise((resolve, reject) => {
      bindFirebaseRef('applications', applicationsRef, {
        readyCallback: (val) => {
          console.log('callback')
          resolve(val)
        }
      })
    })
  }),
  setApplicationRef: firebaseAction(({bindFirebaseRef}, payload) => {
    bindFirebaseRef('application', db.ref('/applications/' + payload.key))
  })
}
  // createApplication: ({commit}, payload) => {
  //   return new Promise((resolve, reject) => {
  //     applicationsRef.push(payload).then(snap => {
  //       resolve(snap.key)
  //     }, error => {
  //       reject(error)
  //     })
  //   })
  // }
// }

export default {
  mutations,
  state,
  getters,
  actions
}

My console is logging:
undefined
callback

@lgertel
Copy link
Author

lgertel commented May 10, 2017

I'll try to add this to jsfiddle

@lgertel
Copy link
Author

lgertel commented May 10, 2017

@posva can i add you on some message channel? Really interested in this project and using for a new software.
Ty

@posva
Copy link
Owner

posva commented May 10, 2017

Sorry but no, I cannot help you out with your personal project. I'll, however, do my best to address any issue you find while developing the app using vuexfire so everyone can benefit 🙂

@lgertel
Copy link
Author

lgertel commented May 10, 2017

https://jsfiddle.net/lgertel/6w3ks04x/6/

Getting crazy with this. Not sure if i'm doing something wrong.

@posva
Copy link
Owner

posva commented May 10, 2017 via email

@lgertel
Copy link
Author

lgertel commented May 10, 2017

Same issue with the promise.
Sorry forgot to paste, but had on my code.

https://jsfiddle.net/lgertel/6w3ks04x/7/

@posva posva added bug and removed need repro labels May 10, 2017
@posva
Copy link
Owner

posva commented May 10, 2017

Thanks, I overlooked a return statement. I'll add it soon

@lgertel
Copy link
Author

lgertel commented May 10, 2017

Great!
Looking forward to the fix.

@posva posva closed this as completed in ace641f May 10, 2017
@posva
Copy link
Owner

posva commented May 10, 2017

Just released it 👍

@lgertel
Copy link
Author

lgertel commented May 10, 2017

Thanks, worked like a charm!

@posva
Copy link
Owner

posva commented May 10, 2017

LGTM. 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants