MagicMirror² v2.4.1 is available! For more information about this release, check out this topic.
Please read the release notes carefully since this update requires adjustments to your Raspberry Pi configuration!

Access the default values in outside functions



  • 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!



  • 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/



  • @MichMich Oh okay, thanks for the help!