在进行查询时出现错误连接结果错误NaN

时间:2013-12-03 11:19:34

标签: node.js express

我试图通过提取对数据库的POST请求的图像名称来进行插入查询


URL:: 54.218.71.244:7002/Details/?key=images

var express=require('express');
var mysql=require('mysql');
var fs=require('fs');
var http=require('http');

var app=express();

var connection=mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'root',
    database:'posting_only_image_DB'
});

connection.connect();

app.set('port',process.env.PORT||7002);
app.use(express.static(__dirname+'/public/images'));
app.use(express.bodyParser());

app.post('/Details/',function(req,res){

    var path=req.files.key.name;

    connection.query('INSERT INTO MyTable(image)',[path],function(err,rows,fields)
    {
        console.log('Connection result error', +err);
    });

});

http.createServer(app).listen(app.get('port'),function(){
    console.log('Express server listening on port'+app.get('port'));
});

我遇到错误 ::

Express server listening on port7002
Connection result error NaN
Error: Request aborted
    at IncomingMessage.<anonymous> (/home/ubuntu/rainmelon/sample_programs/image_express/node_modules/express/node_modules/connect/node_modules/multiparty/index.js:93:17)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at abortIncoming (http.js:1886:11)
    at Socket.serverSocketCloseListener (http.js:1898:5)
    at Socket.EventEmitter.emit (events.js:117:20)
    at TCP.close (net.js:451:12)

1 个答案:

答案 0 :(得分:2)

您正在尝试将err转换为数字:

console.log('Connection result error', +err);
                                       ^

要么使用它:

if (err) {
  console.log('Connection result error: ' + err);
}

或者这(推荐);

if (err) {
  console.log('Connection result error:', err);
}

我也没有看到你结束请求(使用res.send()res.render或其他),你应该这样做。