在使用Express 4,node和angular JS获取MYSQL存储过程的数据时遇到问题。
TypeError: Cannot read property 'query' of undefined
at r.$scope.getInfo (http://localhost:3000/javascripts/controller.js:96:47)
at fn (eval at <anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:213:110), <anonymous>:4:212)
at e (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:254:74)
at r.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:133:313)
at r.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:134:17)
at HTMLButtonElement.<anonymous> (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:254:126)
at If (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:35:367)
at HTMLButtonElement.Hf.d (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:35:314)(anonymous function) @ angular.js:12520
模型/ connection.js
var mysql = require('mysql');
//console.log("yess");
var MySQLConfig = require('../config/config').MySQLConfig;
exports.createConnection = function () {
var connection = mysql.createConnection({
host: MySQLConfig.host,
port: MySQLConfig.port,
user: MySQLConfig.user,
password: MySQLConfig.password,
database: MySQLConfig.database
});
//console.log("yess");
console.log(connection);
return connection;
};
公共/ JS / controller.js
var app = angular.module('app', ['ui.router', 'ui.bootstrap', 'ngResource']);
// configure our routes
app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/dashboard');
$stateProvider
.state('profile', {
url: '/profile',
redirectTo: 'profile.business-information',
templateUrl: 'templates/profile.html',
controller: 'ProfileController'
})
.state('profile.business-information', {
url: "/business-information",
templateUrl: "templates/profileViews/businessInformation.html" ,
controller: 'BusinessInfoController'
});
}]);
app.run(['$rootScope', '$state',
function($rootScope, $state) {
$rootScope.$on('$stateChangeStart',
function(evt, to, params) {
if (to.redirectTo) {
evt.preventDefault();
$state.go(to.redirectTo, params);
}
}
);
}]);
app.controller('ProfileController', ['$scope', function($scope) {
$scope.dropLink = '1';
$scope.dropContentLink = '1';
}]);
app.factory('infoService', ['$resource', function($resource){
//return null;
return $resource('/api/business-information/:id');
}]);
app.controller('BusinessInfoController', ['$scope', function($scope, infoService) {
$scope.isActive = 1;
$scope.is_active_label = 'Active';
$scope.getInfo = function() {
console.log("hiiii");
console.log($scope.businessInformation.industryID);
console.log(infoService);
$scope.businessInformation = infoService.query('BB333');
};
}]);
路由/ api.js
var express = require('express');
var router = express.Router();
var DBManager = require('../models/connection');
router.route('/business-information/:id')
.get(function(req, res){
var conn = DBManager.createConnection();
conn.connect(function(err){
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + conn.threadId);
});
conn.query('Industrys_info_Get(?)', [req.params.id],function(err,info){
if(err) res.send(err);
res.json(info);
console.log('Data received from Db:\n');
console.log(info[0]);
});
}) ;
module.exports = router;
html文件:
<div class='col-lg-6 right_content_BI'>
<button ng-click='getInfo()' class='edit_btn reset_btn pull-right'></button>
<input class='industryID pull-right' ng-model="businessInformation.industryID" type='text' id='industryName' placeholder='BEE2034'/>
</div>
你能帮我找一下这个问题吗?
谢谢