使用node和AngularJS在mysql中插入数据时出错

时间:2015-12-09 13:32:54

标签: javascript mysql angularjs node.js

我只是使用node和angularjs将数据插入MySQL数据库,我在使用express来路由指定的URL以进行插入时遇到500内部错误。

这是我的节点和角度代码,如果代码中有任何错误,请纠正我。

节点Js(server.js)

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());       
app.use(bodyParser.urlencoded({extended: true})); 

var mysql = require("mysql");
var http = require("http");
var path = require("path");

/*
  * Configure MySQL parameters.
*/
var connection      =         mysql.createConnection({
        host        :         "localhost",
        user        :         "root",
        password    :         "",
        database     :         "two_way_demo"
});

connection.connect(function(error){
  if(error)
    {
      console.log("Problem with MySQL"+error);
    }
  else
    {
      console.log("Connected with Database");
    }
});

/*
  * Configure Express Server.
*/
app.use(express.static(__dirname + "/angular"));
/*
  * Define routing of the application.
*/
app.get("/",function(req,res){
    console.log(req.body);    
    res.sendfile("index.html");
});
app.post("/addd",function(req,res){
    console.log(req.body);
    /* TODO: Now just check that your drive function is correct, SQL is correct and whether what arguements passed to SQL callback is correct */
    mysql.query('Insert into user_info (profile_picture,news) VALUES (""+req.body.pics+"",""+req.body.feed+"")',function(err, results, fields) {
        //if (err) throw err;
        if (err) {console.log("DB Error"); res.send("add failed");}
        else res.send("add success");
    });
    //res.send("success");
});
/*
  * Start the Express Web Server.
*/
app.listen(3000,function(){
    console.log("It's Started on PORT 3000");
});

Angular js

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
#add_cont{
    margin-top: 118px;
    margin-left: 498px;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body>
<div class="container" ng-app="two_way" ng-controller="two_way_control">
<form class="form-inline" id="add_cont">
<input type="text" name="feed" ng-model="news.feed" class="form-control" placeholder="Enter News"><br><br>
<input type="text" name="pics" ng-model="news.pics" class="form-control" placeholder="Enter Image URL"><br><br>
<input type="submit" class="btn btn-success" value="Add" ng-click="submit(news)">
</form>
</div>
<script>
var app=angular.module("two_way",[]);
app.controller("two_way_control",function($scope,$http,$interval){
$scope.news={};
/* put $http into a service. Keep controllers thin */
$scope.submit = function(newpost){
    /* processData:false why do you need this? */
    /* I can see the code behind this $interval */
        console.log("triggering post");
        $http({method:"post", url:"http://localhost:3000/addd", data:newpost})
            .then(function(data){
                /* Success callback */
                console.log("success");
            },function(err){
                /* Failure callback */
                console.log("failed");
            });
    };
});
</script>    
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在服务器端代码中,您应该使用 connection.query 来替换 mysql.query

app.post("/addd",function(req,res){
    console.log(req.body);
    /* TODO: Now just check that your drive function is correct, SQL is correct and whether what arguements passed to SQL callback is correct */
    mysql.query('Insert into user_info (profile_picture,news) VALUES (""+req.body.pics+"",""+req.body.feed+"")',function(err, results, fields) {
        //if (err) throw err;
        if (err) {console.log("DB Error"); res.send("add failed");}
        else res.send("add success");
    });
    //res.send("success");
});