TypeError:尝试将节点js连接到mysql以获取zendesk应用程序时,无法读取null的属性“createConnection”

时间:2017-07-28 05:44:35

标签: mysql node.js

我是节点的新手,我正在尝试创建一个zendesk应用程序,当用户输入一个值时,该应用程序连接到mysql。这是app.js的代码。

( function() {
  return {

    events: {
      // Framework Events
      'app.created':'checkLocalStorage', // Call 'checkLocalStorage' function below

      // Request Events (None)

      // DOM events
      'keyup #input-value-id': function(event){ // User clicked the 'enter'/'return' button so store value in localStorage
        if(event.keyCode === 13) {

          return this.processInputValue();
        }
      },
      'click .reset': 'resetValue' // Call 'resetValue' function below
    },

    checkLocalStorage: function () { // Look in localStorage for if value is present for 'exampleString'
        if (this.store('exampleString') === null) {

          services.notify('localStorage value not set', 'alert', 500); // Services notification

          this.switchTo('input_value'); // Display input field for user to set value of localStorage

        } else { // There is a value already set for 'exampleString' in localStorage

          services.notify('localStorage value found', 'notice', 500); // Services notification

          var value = this.store('exampleString'); // Instantiate variable 'value' set to value in localStorage 'exampleString'

          this.switchTo('display_value', { // Switch to 'displayValue' template
            value: value // Set handlebars.js placeholder 'value' equal to the variable 'value' above
          });

        }
    },

    processInputValue: function() { // Handle value entered into input field in the 'inputValue' template
      var exampleString = this.$('input#input-value-id').val(); // Variable set to value entered into input field
      this.store('exampleString', exampleString); // Value of exampleString for future use

      exampleString = this.$('input#input-value-id').val(''); // Empty input field - visual UX enhancement

      services.notify('localStorage value set', 'notice', 500); // Services notification

      var value = this.store('exampleString');
      this.switchTo('display_value', {
        value: value
      });
          var mysql = require('mysql');
          var con = mysql.createConnection({
              host: "localhost",
              user: "root",
              password: "WayPass123"
          });
          con.connect(function(err) {
              if (err) throw err;
              console.log("Connected!");
              con.query("SELECT * FROM way0608.user_info", function (err, result, fields) {
                  if (err) throw err;
                    console.log(result);
              });
          });

    },

    resetValue: function() {

      this.store('exampleString', null); // Set value of 'exampleString' in localStorage to 'empty' variable (null)

      services.notify('localStorage value reset', 'notice', 500); // Services notification

      this.switchTo('input_value'); // Display input field for user to set value of localStorage

    }

  };

}());

当我在自己的js文件中运行代码的mysql部分时,如下所示,它可以正常工作。

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "pass"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  con.query("SELECT * FROM test.user_info", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

但是,我只在zendesk上运行app.js时得到TypeError。我还测试了在zendesk上运行一个示例应用程序,它运行良好。这意味着有时mysql连接代码有问题。

0 个答案:

没有答案