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

我是新来的节点,我试图创build一个连接到MySQL时,当用户input一个值的zendesk应用程序。 这是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。 我也testing了在zendesk上运行一个示例应用程序,工作正常。 这意味着某些时候mysql连接代码是错误的。