1

I am trying to display a pop up notification on a successful promise, but I keep getting "ReferenceError: doNotification is not defined". If I trigger doNotification on button click within my html, it works. My email function works as well, as console.log prints the appropriate parameters to the console. I am not sure why I am getting this error when calling doNotification from within my email function?

export default {
  methods: {

    sendEmail: (e) => {
      emailjs.sendForm('gmail', 'template', e.target, 'user')
        .then((result) => {
            console.log('SUCCESS!', result.status, result.text);
            doNotification();
        }, (error) => {
            console.log('FAILED...', error);
        });
    e.target.reset();
    },

    doNotification() {
      this.$toast.success("Your message has been sent!", {
      position: "bottom-right"});
    }}}
eneemz
  • 21
  • 4
  • 1
    Change `sendEmail: (e) => {` to `sendEmail(e) {` and use `doNotification` like so: `this.doNotification();` – ibrahim mahrir Oct 28 '20 at 17:20
  • Thank you! This works. What does changing sendEmail: (e) => { to sendEmail(e) { do? – eneemz Oct 28 '20 at 17:43
  • 1
    The only way you can access `doNotification` from `sendEmail` is via the `this` keyword, because both of those methods are attached to the same object, and `this` will refer to that object. With that said, `sendEmail` needs to be a regular function (either `sendEmail(e) {` or `sendEmail: function(e) {`) because arrow functions (like `sendEmail: e => {`) don't have their own `this`, so `this.doNotification()` inside the arrow version of `sendEmail` won't work. – ibrahim mahrir Oct 28 '20 at 17:59
  • ...read more about this difference here: https://stackoverflow.com/q/34361379/9867451 – ibrahim mahrir Oct 28 '20 at 18:00
  • Thank you so much! – eneemz Oct 28 '20 at 18:53
  • You're welcome! I'm glad I could help :) – ibrahim mahrir Oct 28 '20 at 18:54

1 Answers1

0

you are missing this binding. change doNotification() to this.doNotification()

Dhruvi Makvana
  • 895
  • 5
  • 17