错误:POST http:// localhost:3000 / api / createProductCategory 500(内部服务器错误)

时间:2016-04-08 11:00:24

标签: mysql angularjs node.js express

我正在尝试使用POST方法将数据插入到表中,因为我有一个角度服务函数

angular.module("productCategoryModule")
.factory("productCategoryService",productCategoryService);

productCategoryService.$inject = ['$http'];

function productCategoryService($http){
    return {
        createProductCategory:function(productCategory){
            console.log("createProductCategory in service called",productCategory);
            return $http.post('/api/createProductCategory',
                {
                    categoryName:productCategory.categoryName,
                    details:productCategory.categoryDetails
                }
            );
        },
        getAllProductCategories:function(){
            return $http.get('/api/getAllProductCategory');
        }
    }
}

在服务器端我有

function productCategoryRouteConfig(app){
    this.app = app;
    this.routeTable = [];
    this.init();
}

productCategoryRouteConfig.prototype.init = function(){
    var self = this;
    this.addRoutes();
    this.processRoutes();
}

productCategoryRouteConfig.prototype.processRoutes = function(){
    var self = this;
    self.routeTable.forEach(function(route){
        if(route.requestType == 'get'){
            //console.log("requestType",route.requestType)
            self.app.get(route.requestUrl,route.callbackFunction);
        } else if(route.requestType == 'post'){
            //console.log("requestType",route.requestType);
            self.app.post(route.requestUrl,route.callbackFunction);
        } else if(route.requestType == 'delete'){

        }

    });
}

productCategoryRouteConfig.prototype.addRoutes = function(){
    var self = this;
    self.routeTable.push({
        requestType: 'get',
        requestUrl: '/createProductCategory',
        callbackFunction: function(req, res){
            res.render('createProductCategory',{title:'Create Product Category'});
        }
    });

    self.routeTable.push({
        requestType: 'post',
        requestUrl: '/api/createProductCategory',
        callbackFunction: function(req, res){
            console.log("Post called");
            //res.render('createProductCategory');
            console.log("req.body",req.body);
            var productCategoryDb = require('../database/productCategoryDb');
            // console.log("productCategoryDb post",productCategoryDb);
            // console.log("hello from createProductCategory post");
            // console.log("req.body",req.body);

            // productCategoryDb.productCategoryDb.createProductCategory(req.body, function(status){
            //  if(status)
            //      res.json(status);
            //  console.log(status);
            // });
        }
    });

    self.routeTable.push({
        requestType: 'get',
        requestUrl: '/viewProductCategory',
        callbackFunction: function(req, res){
            res.render('viewProductCategory',{title:'View Product Category'});
        }
    });

    self.routeTable.push({
        requestType: 'get',
        requestUrl: '/api/getAllProductCategory',
        callbackFunction: function(req, res){
            console.log("hello from getAllProductCategory");
            var productCategoryDb = require('../database/productCategoryDb');
            console.log("productCategoryDb",productCategoryDb);
            // productCategoryDb.productCategoryDb.getAllProductCategories(
            //  function (productCategories){
            //      console.log("productCategories",productCategories);
            //      res.json({productCategories : productCategories});
            //  }
            // );
        }
    });
}

module.exports = productCategoryRouteConfig;

当我点击客户端的按钮时,我收到此错误

POST http://localhost:3000/api/createProductCategory 500(内部服务器错误)

我正在使用Node express mysql和angular。

我的数据库文件夹中有三个文件。

1.connectionString.js

var mysqlConnectionString = {
    connectionString:{
        host:'localhost',
        user:'root',
        password:'root',
        database:'vidzy'
    }
}

//module.exports = mysqlConnectionString;
exports.mysqlConnectionString = mysqlConnectionString;

2.connection.js

var mysql = require('mysql');
var mysqlConnectionString = require('/home/ep-3/node-express/yt_tutorial/database/connectionString.js');
var connectionStringProvider = {
    getSqlConnection:function(){
        var connection = mysql.createConnection(mysqlConnectionString.mysqlConnectionString.connectionString);

        connection.connect(function(err){
            if(err){
                throw err;
            } else{
                console.log("connection was successful");
            }
        });

        return connection;
    },
    closeSqlConnection:function(currentConnection){
        currentConnection.end(function(err){
            if(err){
                throw err;
            } else{
                console.log("Disconnected");
            }
        })
    }
}

exports.connectionStringProvider = connectionStringProvider;

3.productCategoryDb.js

var connectionProvider = require('/home/ep-3/node-express/yt_tutorial/database/connection.js');
var productCategoryDb = {

    createProductCategory : function(productCategory, onSuccessful){

        var insertStatement = 'INSERT INTO productcategory SET?';

        var category = {
            categoryName : productCategory.categoryName,
            Details : productCategory.details,
            isValid : productCategory.isValid,
            CreatedDate : new Date()
        }

        var connection = connectionProvider.connectionStringProvider.getSqlConnection();

        if(connection){

            connection.query(insertStatement, category, function(err, result){

                if(err){

                    console.log(err);
                }

                onSuccessful({status : 'Successful'});
                console.log(result); 
            });

            connectionProvider.connectionStringProvider.closeSqlConnection(connection);
        }
    },

    getAllProductCategory : function(callback){
        var connection = connectionProvider.connectionStringProvider.getSqlConnection();
        var selectStatement = 'SELECT * FROM productcategory';

        if(connection){
            connection.query(selectStatement, function(err, rows, fields){
                if(err){ through err; }
                console.log(rows);
                callback(rows);
            });
        }
    }
}

exports.productCategoryDb = productCategoryDb;

2 个答案:

答案 0 :(得分:2)

您确定已加入模块body-parser

您在此处发布的代码似乎与我一直关注的教程相同。

您的代码似乎没问题,但我不知道app.js中的代码是什么样的。

我已经确认当我注释掉模块undefined时,我将 req.body 的控制台响应设为body-parser

答案 1 :(得分:0)

我遇到了同样的错误,但在我的情况下,有一个节点模块功能,它为我所做的每个POST增加ID,而我忘记在DTO中添加ID。也许这不是您的情况,因为您正在使用mysql,但无论如何我都会发布答案,也许some1将通过此帮助解决他的错误