gpt4 book ai didi

mysql - 错误: POST http://localhost:3000/api/createProductCategory 500 (Internal Server Error)

转载 作者:行者123 更新时间:2023-11-29 07:20:07 27 4
gpt4 key购买 nike

我正在尝试使用 POST 方法将数据插入表中,因为我有一个 angular.service 函数

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;

当我单击客户端的按钮时,出现此错误

发布 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;

最佳答案

您确定已包含模块body-parser吗?

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

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

我已经验证,当我注释掉模块 body-parser 时,我得到的 req.body 控制台响应为 未定义

关于mysql - 错误: POST http://localhost:3000/api/createProductCategory 500 (Internal Server Error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36497967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com