1

I would like to set data of a vue.js component with a button template and @click event. This event call a method component which call a async function with promise.

When I click on my button the result of paragraph tag html appear correctly but in a same time it generated a error into my inspector browser electron like this :

vue.runtime.esm.js:620 [Vue warn]: Error in v-on handler: "TypeError: foo.then(...).bind is not a function"
found in
---> at src/views/TestDb.vue
at src/App.vue

Very strange to understand why throw error and in same time do the job....

I now the trick of copy the context this into another variable and call this variable for change data component but it seems to be not advise by esLint process analyse file. It exist a specific rule just for that.

How can work of basic async promise for change data with a click button template on vuejs ?

This is my component file :

<template>

    <div id="testDb">
        <Menu />
        <h2>Test DB operation</h2>
        <b-button @click="createOperation">create Operation</b-button>
        <p style="background-color: white">{{ returnValue1 }}</p>
    </div>

</template>

<script>

import Menu              from "@/components/Menu";
import ConnectionManager from '../service/ConnectionManager'

export default
{
    name: "TestDb",
    components:
    {
        Menu
    },
    data: function()
    {
        return {
            alert: null,
            returnValue1: "beer"
        }
    },
    methods:
    {
        createOperation: function ()
        {
            const connectionManager = new ConnectionManager()
            let foo = connectionManager.insert('operation')
            foo.then(() => {
                this.returnValue1 = "bar"
            }).bind(this)
        },
    }
}

</script>

<style lang="scss" scoped>

    @import './src/assets/scss/main';

</style>

2 Answers 2

3

Since you're using an arrow function there's no need to add bind(this) :

 foo.then(() => {
                this.returnValue1 = "bar"
            })

or

 foo.then(function(){
                this.returnValue1 = "bar"
            }.bind(this))
Sign up to request clarification or add additional context in comments.

1 Comment

OK i search many others thing but not about arrow function. Thank you for your very speed response. I confirm that's work well. Perhaps this little issue help others.
1

Remove the bind ' the arrow function already did the binding.

foo.then(() => {
                this.returnValue1 = "bar"
            })

FYI

You could have done this before

foo.then(() => {
                this.returnValue1 = "bar"
            }.bind(this))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.