MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Access the default values in outside functions

    Scheduled Pinned Locked Moved Solved Development
    3 Posts 2 Posters 2.0k Views 1 Watching
    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 Offline
      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
      • MichMichM Offline
        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 Offline
          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 Sam, technical setup by Karsten.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy