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!


  • MagicMirror Creator

    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!


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.