MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord
    MagicMirror² v2.24.0 is available! For more information about this release, check out this topic.

    SOLVED Access the default values in outside functions

    Development
    2
    3
    1770
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      gndimitro last edited by gndimitro

      I’m having troubles accessing my default values in my hello function, in my start function the values exist but in hello they are undefined and unreadable, code is below:

      Module.register("jarvis", {
      // Module defaults
        defaults: {
          assistantName: "jarvis",
          assistantVoice: "UK English Male"
        },
      
        // Required Scripts
        getScripts: function() {
      	return ["annyang.js",
              "http://code.responsivevoice.org/responsivevoice.js"];
      },
      
      hello: function(name) {
      
          if(name)
            name = name.trim().toLowerCase();
      
      Log.info(name)
      Log.info(this.config.assistantName)
      
      if(name && (name == this.config.assistantName))
        responsiveVoice.speak("Hello George");
      else {
        responsiveVoice.speak("Hello");
      }
        },
      
        start: function() {
        Log.info("Starting module: " + this.name);
        annyang.debug();
          if (annyang) {
            // Let's define a command.
            var commands = {
              'hello (*name)': this.hello
            };
      
            // Add our commands to annyang
            annyang.addCommands(commands);
      
            // Start listening.
            annyang.start();
      }
      
      Log.info(this.config.assistantVoice)
      Log.info(this.config.assistantName)
      
      // Setting voice default
      responsiveVoice.setDefaultVoice(this.config.assistantVoice);
        }
      });
      

      Please help, thank you!

      1 Reply Last reply Reply Quote 0
      • MichMich
        MichMich Admin last edited by

        That’s because “this” is no longer referring to your instance. It’s a common javascript pitfall. Check out:
        http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/

        G 1 Reply Last reply Reply Quote 0
        • G
          gndimitro @MichMich last edited by

          @MichMich Oh okay, thanks for the help!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy