未知列' temp'在'字段列表'

时间:2014-08-07 02:34:57

标签: javascript mysql node.js ejs gpio

我想在我的Temperature REST API上寻求帮助。我刚刚在两周前启动了node.js,并设法创建一个基本的REST API,获取 DS18B20温度传感器 ID和读取。能够完美地工作而没有任何错误或问题,但当我开始实现将我的SQL数据库存储到我的index.js并执行app.js的功能时,我收到了一个错误,我现在迷路了。

希望你们能尽快帮我解决这个问题,先谢谢你们。

这是我的app.js:

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var app = express();

// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 3000);

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);

// Catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;
var server = app.listen(app.get('port'), function() {
  console.log('Listening on port %d', server.address().port);
});

这是我的/routes/index.js:

var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var gpio = require('pi-gpio');
var sensor = require('ds18b20');

var conn = {
    host: 'localhost',
    user: 'user',
    password: 'password',
    database: 'rest_api',
    port: '3306'
};

router.get('/', function(req, res) {
    var pin = req.params.pin;
    var connection = mysql.createConnection(conn);

    //Get ID and Reading from DS18B20 Temperature Sensor 
    sensor.getAll(function (err, tempObj) {
        console.log(tempObj);
    });

    //Store to mySQL database
    sensor.get('10-000802be4633', function (err, temp) {
        connection.query('INSERT INTO sensorLog(sensorId, logTime, sensorValue) VALUES(4, current_timestamp(), temp)', function(err) {
            if (err) {
                throw err;
            } else {
                console.log("Successfully store to mySQL database");
            }
        });
});

});

module.exports = router;

这是我的/views/index.ejs:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
  </body>
</html>

这是我的错误消息:

/var/www/node_modules/mysql/lib/protocol/Parser.js:82
        throw err;
              ^
Error: ER_BAD_FIELD_ERROR: Unknown column 'temp' in 'field list'
at Query.Sequence._packetToError (/var/www/node_modules/mysql/lib/protocol/s                                                                                                                                                             equences/Sequence.js:48:14)
at Query.ErrorPacket (/var/www/node_modules/mysql/lib/protocol/sequences/Que                                                                                                                                                             ry.js:82:18)
at Protocol._parsePacket (/var/www/node_modules/mysql/lib/protocol/Protocol.                                                                                                                                                          js:251:24)
at Parser.write (/var/www/node_modules/mysql/lib/protocol/Parser.js:77:12)
at Protocol.write (/var/www/node_modules/mysql/lib/protocol/Protocol.js:39:1                                                                                                                                                             6)
at Socket.<anonymous> (/var/www/node_modules/mysql/lib/Connection.js:82:28)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
    --------------------
at Protocol._enqueue (/var/www/node_modules/mysql/lib/protocol/Protocol.js:1                                                                                                                                                             35:48)
at Connection.query (/var/www/node_modules/mysql/lib/Connection.js:184:25)
at /var/www/routes/index.js:38:15
at /var/www/node_modules/ds18x20/lib/ds18x20.js:47:20
at /var/www/node_modules/ds18x20/node_modules/async/lib/async.js:232:13
at /var/www/node_modules/ds18x20/node_modules/async/lib/async.js:119:25
at /var/www/node_modules/ds18x20/node_modules/async/lib/async.js:24:16
at /var/www/node_modules/ds18x20/node_modules/async/lib/async.js:229:17
    at /var/www/node_modules/ds18x20/lib/ds18x20.js:42:24
    at /var/www/node_modules/ds18x20/lib/reader.js:29:10

1 个答案:

答案 0 :(得分:2)

我相信当你完成销钉时你需要gpio.close(...)

E.g。

router.get('/:pin/1', function(req, res) {
    // ...
    gpio.open(pin, 'output', function(err) {
        // ...
        gpio.close(pin, function(err) {
            // done
        });
    });
});

否则,该引脚可能仍会从先前的请求中打开。

修改

此外,您需要在SQL查询中使用变量temp的值,而不是字面temp。现在,您的查询看起来像这样:

INSERT INTO sensorLog (sensorId, logTime, sensorValue)
   VALUES (4, current_timestamp(), temp)

您实际需要使用的是:

INSERT INTO sensorLog (sensorId, logTime, sensorValue)
   VALUES (4, current_timestamp(), 0)

也就是说,SQL查询需要包含一个数字。数据库不知道temp的含义;它认为你指的是一个列,但是没有一个具有该名称的列。

您需要更改查询以使用temp的实际值,如下所示:

connection.query('INSERT INTO sensorLog (sensorId, logTime, sensorValue) '
   + 'VALUES (4, current_timestamp(), ' + temp + ')', function(err) {

   // function body
});