NodeJS :: TypeError:无法调用undefined的方法'query'

时间:2015-02-02 12:15:35

标签: node.js node-mysql

有人可以帮我这个。我一直在搜索,无法得到明确的解决方案。

database.js

var MySql           = require('mysql');
var moment          = require('moment');

var connection = MySql.createConnection({
   host         : dbHost,
   user         : Username,
   password     : MysQlPassword,
   database     : dbName
});

connection.connect( function( err){
    if (err) {
        console.log( "There was a problem " + err );
    } else {
        console.log( "Database Connection successful,");
    }
});

在我的产品页面路线上,我有以下内容,我将在这里有其他方法来创建,更新,删除和获取单个产品

product.js

var dbConnect = require('../config/database');

var products = {
 create: function(req, res) {},

 getAll: function(req, res){

  var data = dbConnect.connection.query("SELECT * FROM products ", function( err, result){
    console.log(err);
    console.log(result);
   });
 var allProducts = data;
 res.json(allProducts);

 }


};

module.exports = products;

在控制台上我有以下错误。

TypeError: Cannot call method 'query' of undefined
at products.getAll (/home/anele/Desktop/myApp/server/routes/products.js:8:37)

1 个答案:

答案 0 :(得分:0)

ANSWER

根据@ben-fortune

var MySql    = require('mysql'),
    moment   = require('moment');

var connection = MySql.createConnection({
    host         : dbHost,
    user         : Username,
    password     : MysQlPassword,
    database     : dbName
});

connection.connect( function( err){
   if (err) {
     console.log( "There was a problem " + err );
   } else {
    console.log( "Database Connection successful,");
   }
});

// I was missing the code bellow
module.exports = connection;