Dispatch receiving undefined as a value










1















I have issue updating the state after a dispatch.



Console logging the result of the dispatch shows Promise pending and value undefined hence, never gets to the store.



console result



Below is the function that calls the dispatch handler.



unsetSelected() 
let some = this.$store.dispatch('user/selectedDevice', null)
console.log(some)



<span class="ellipsis" @click="setSelected(device)">
<i v-if="selectedDevice && selectedDevice.id == device.id"
@click="unsetSelected()"
class="fa fa-times-circle">
</i>
<i v-else="" class="fa fa-ellipsis-v"></i>
</span>


This is the action handler:



selectedDevice ( commit , data) 
commit ('SELECTED_DEVICE', data);










share|improve this question


























    1















    I have issue updating the state after a dispatch.



    Console logging the result of the dispatch shows Promise pending and value undefined hence, never gets to the store.



    console result



    Below is the function that calls the dispatch handler.



    unsetSelected() 
    let some = this.$store.dispatch('user/selectedDevice', null)
    console.log(some)



    <span class="ellipsis" @click="setSelected(device)">
    <i v-if="selectedDevice && selectedDevice.id == device.id"
    @click="unsetSelected()"
    class="fa fa-times-circle">
    </i>
    <i v-else="" class="fa fa-ellipsis-v"></i>
    </span>


    This is the action handler:



    selectedDevice ( commit , data) 
    commit ('SELECTED_DEVICE', data);










    share|improve this question
























      1












      1








      1








      I have issue updating the state after a dispatch.



      Console logging the result of the dispatch shows Promise pending and value undefined hence, never gets to the store.



      console result



      Below is the function that calls the dispatch handler.



      unsetSelected() 
      let some = this.$store.dispatch('user/selectedDevice', null)
      console.log(some)



      <span class="ellipsis" @click="setSelected(device)">
      <i v-if="selectedDevice && selectedDevice.id == device.id"
      @click="unsetSelected()"
      class="fa fa-times-circle">
      </i>
      <i v-else="" class="fa fa-ellipsis-v"></i>
      </span>


      This is the action handler:



      selectedDevice ( commit , data) 
      commit ('SELECTED_DEVICE', data);










      share|improve this question














      I have issue updating the state after a dispatch.



      Console logging the result of the dispatch shows Promise pending and value undefined hence, never gets to the store.



      console result



      Below is the function that calls the dispatch handler.



      unsetSelected() 
      let some = this.$store.dispatch('user/selectedDevice', null)
      console.log(some)



      <span class="ellipsis" @click="setSelected(device)">
      <i v-if="selectedDevice && selectedDevice.id == device.id"
      @click="unsetSelected()"
      class="fa fa-times-circle">
      </i>
      <i v-else="" class="fa fa-ellipsis-v"></i>
      </span>


      This is the action handler:



      selectedDevice ( commit , data) 
      commit ('SELECTED_DEVICE', data);







      vue.js vuex nuxt






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 13:56









      obi patrickobi patrick

      214




      214






















          2 Answers
          2






          active

          oldest

          votes


















          1














          I put alert on the mutation handler and discovered that the code was working as expected only that it was also firing the dispatch function above in the DOM.



          I have to chain it with .stop modifier: @click.stop="unsetSelected()"






          share|improve this answer






























            0














            From Vuex API reference for dispatch instance methods:




            Returns a Promise that resolves all triggered action handlers.




            So calling dispatch returns a Promise. In order to receive data inside component, this promise should be resolved.



            You can modify component's method to following:



            // using async/await
            async unsetSelected()
            try
            let some = await this.$store.dispatch('user/selectedDevice', null)
            console.log(some)
            catch (error)
            // handle error



            // using Promise API
            unsetSelected()
            this.$store.dispatch('user/selectedDevice', null)
            .then((some) =>
            console.log(some)
            )
            .catch((error) =>
            // handle error
            )



            Also, selectedDevice does not return any data, so some (or response) from resolved promise will be undefined for example code provided in question.



            To fix that, store action should have a return statement with desired data to return to the component.



            Though, following Vuex architecture, it would be suggested to map state/getters, whose values will be reactively updated after state mutation has been committed.






            share|improve this answer

























            • All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

              – obi patrick
              Nov 14 '18 at 14:21











            • Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

              – aBiscuit
              Nov 14 '18 at 14:25












            • I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

              – obi patrick
              Nov 14 '18 at 14:52











            • computed: selectedDevice() return this.$store.getters['user/selectedDevice']

              – obi patrick
              Nov 14 '18 at 14:54











            • Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

              – aBiscuit
              Nov 14 '18 at 15:06










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301922%2fdispatch-receiving-undefined-as-a-value%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I put alert on the mutation handler and discovered that the code was working as expected only that it was also firing the dispatch function above in the DOM.



            I have to chain it with .stop modifier: @click.stop="unsetSelected()"






            share|improve this answer



























              1














              I put alert on the mutation handler and discovered that the code was working as expected only that it was also firing the dispatch function above in the DOM.



              I have to chain it with .stop modifier: @click.stop="unsetSelected()"






              share|improve this answer

























                1












                1








                1







                I put alert on the mutation handler and discovered that the code was working as expected only that it was also firing the dispatch function above in the DOM.



                I have to chain it with .stop modifier: @click.stop="unsetSelected()"






                share|improve this answer













                I put alert on the mutation handler and discovered that the code was working as expected only that it was also firing the dispatch function above in the DOM.



                I have to chain it with .stop modifier: @click.stop="unsetSelected()"







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 8:37









                obi patrickobi patrick

                214




                214























                    0














                    From Vuex API reference for dispatch instance methods:




                    Returns a Promise that resolves all triggered action handlers.




                    So calling dispatch returns a Promise. In order to receive data inside component, this promise should be resolved.



                    You can modify component's method to following:



                    // using async/await
                    async unsetSelected()
                    try
                    let some = await this.$store.dispatch('user/selectedDevice', null)
                    console.log(some)
                    catch (error)
                    // handle error



                    // using Promise API
                    unsetSelected()
                    this.$store.dispatch('user/selectedDevice', null)
                    .then((some) =>
                    console.log(some)
                    )
                    .catch((error) =>
                    // handle error
                    )



                    Also, selectedDevice does not return any data, so some (or response) from resolved promise will be undefined for example code provided in question.



                    To fix that, store action should have a return statement with desired data to return to the component.



                    Though, following Vuex architecture, it would be suggested to map state/getters, whose values will be reactively updated after state mutation has been committed.






                    share|improve this answer

























                    • All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

                      – obi patrick
                      Nov 14 '18 at 14:21











                    • Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

                      – aBiscuit
                      Nov 14 '18 at 14:25












                    • I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

                      – obi patrick
                      Nov 14 '18 at 14:52











                    • computed: selectedDevice() return this.$store.getters['user/selectedDevice']

                      – obi patrick
                      Nov 14 '18 at 14:54











                    • Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

                      – aBiscuit
                      Nov 14 '18 at 15:06















                    0














                    From Vuex API reference for dispatch instance methods:




                    Returns a Promise that resolves all triggered action handlers.




                    So calling dispatch returns a Promise. In order to receive data inside component, this promise should be resolved.



                    You can modify component's method to following:



                    // using async/await
                    async unsetSelected()
                    try
                    let some = await this.$store.dispatch('user/selectedDevice', null)
                    console.log(some)
                    catch (error)
                    // handle error



                    // using Promise API
                    unsetSelected()
                    this.$store.dispatch('user/selectedDevice', null)
                    .then((some) =>
                    console.log(some)
                    )
                    .catch((error) =>
                    // handle error
                    )



                    Also, selectedDevice does not return any data, so some (or response) from resolved promise will be undefined for example code provided in question.



                    To fix that, store action should have a return statement with desired data to return to the component.



                    Though, following Vuex architecture, it would be suggested to map state/getters, whose values will be reactively updated after state mutation has been committed.






                    share|improve this answer

























                    • All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

                      – obi patrick
                      Nov 14 '18 at 14:21











                    • Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

                      – aBiscuit
                      Nov 14 '18 at 14:25












                    • I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

                      – obi patrick
                      Nov 14 '18 at 14:52











                    • computed: selectedDevice() return this.$store.getters['user/selectedDevice']

                      – obi patrick
                      Nov 14 '18 at 14:54











                    • Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

                      – aBiscuit
                      Nov 14 '18 at 15:06













                    0












                    0








                    0







                    From Vuex API reference for dispatch instance methods:




                    Returns a Promise that resolves all triggered action handlers.




                    So calling dispatch returns a Promise. In order to receive data inside component, this promise should be resolved.



                    You can modify component's method to following:



                    // using async/await
                    async unsetSelected()
                    try
                    let some = await this.$store.dispatch('user/selectedDevice', null)
                    console.log(some)
                    catch (error)
                    // handle error



                    // using Promise API
                    unsetSelected()
                    this.$store.dispatch('user/selectedDevice', null)
                    .then((some) =>
                    console.log(some)
                    )
                    .catch((error) =>
                    // handle error
                    )



                    Also, selectedDevice does not return any data, so some (or response) from resolved promise will be undefined for example code provided in question.



                    To fix that, store action should have a return statement with desired data to return to the component.



                    Though, following Vuex architecture, it would be suggested to map state/getters, whose values will be reactively updated after state mutation has been committed.






                    share|improve this answer















                    From Vuex API reference for dispatch instance methods:




                    Returns a Promise that resolves all triggered action handlers.




                    So calling dispatch returns a Promise. In order to receive data inside component, this promise should be resolved.



                    You can modify component's method to following:



                    // using async/await
                    async unsetSelected()
                    try
                    let some = await this.$store.dispatch('user/selectedDevice', null)
                    console.log(some)
                    catch (error)
                    // handle error



                    // using Promise API
                    unsetSelected()
                    this.$store.dispatch('user/selectedDevice', null)
                    .then((some) =>
                    console.log(some)
                    )
                    .catch((error) =>
                    // handle error
                    )



                    Also, selectedDevice does not return any data, so some (or response) from resolved promise will be undefined for example code provided in question.



                    To fix that, store action should have a return statement with desired data to return to the component.



                    Though, following Vuex architecture, it would be suggested to map state/getters, whose values will be reactively updated after state mutation has been committed.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 '18 at 14:14

























                    answered Nov 14 '18 at 14:08









                    aBiscuitaBiscuit

                    1,4981614




                    1,4981614












                    • All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

                      – obi patrick
                      Nov 14 '18 at 14:21











                    • Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

                      – aBiscuit
                      Nov 14 '18 at 14:25












                    • I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

                      – obi patrick
                      Nov 14 '18 at 14:52











                    • computed: selectedDevice() return this.$store.getters['user/selectedDevice']

                      – obi patrick
                      Nov 14 '18 at 14:54











                    • Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

                      – aBiscuit
                      Nov 14 '18 at 15:06

















                    • All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

                      – obi patrick
                      Nov 14 '18 at 14:21











                    • Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

                      – aBiscuit
                      Nov 14 '18 at 14:25












                    • I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

                      – obi patrick
                      Nov 14 '18 at 14:52











                    • computed: selectedDevice() return this.$store.getters['user/selectedDevice']

                      – obi patrick
                      Nov 14 '18 at 14:54











                    • Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

                      – aBiscuit
                      Nov 14 '18 at 15:06
















                    All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

                    – obi patrick
                    Nov 14 '18 at 14:21





                    All you have suggested I have done but still returns undefined. I have getters setup for the selectedDevice still not working. @aBiscuit

                    – obi patrick
                    Nov 14 '18 at 14:21













                    Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

                    – aBiscuit
                    Nov 14 '18 at 14:25






                    Were you able to resolve the Promise, but it still shows undefined? Where do you expect to receive value?

                    – aBiscuit
                    Nov 14 '18 at 14:25














                    I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

                    – obi patrick
                    Nov 14 '18 at 14:52





                    I want to receive in the store export const state = () =>( devices: null, selectedDevice: null ) Then have it inthe component via computed property

                    – obi patrick
                    Nov 14 '18 at 14:52













                    computed: selectedDevice() return this.$store.getters['user/selectedDevice']

                    – obi patrick
                    Nov 14 '18 at 14:54





                    computed: selectedDevice() return this.$store.getters['user/selectedDevice']

                    – obi patrick
                    Nov 14 '18 at 14:54













                    Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

                    – aBiscuit
                    Nov 14 '18 at 15:06





                    Please, add SELECTED_DEVICE mutation code to the question to verify that everything is being updated.

                    – aBiscuit
                    Nov 14 '18 at 15:06

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301922%2fdispatch-receiving-undefined-as-a-value%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto