2014-12-02 124 views
0

尝试在Sails.js控制器的模型上调用.create()时出现以下错误。在Sails.js中使用水线ORM时ER_BAD_FIELD_ERROR

enter image description here

这里是我的模型,TemperatureReading.js:

module.exports = { 
    connection: 'mainDatabase', 
    tableName: 'TemperatureReading', 

    attributes: { 

     id: { 
     columnName: 'id', 
     type: 'integer', 
     minLength: 1, 
     primaryKey: true, 
     unique: true 
     }, 

     deviceId: { 
     columnName: 'deviceId', 
     type: 'integer', 
     minLength: 1, 
     }, 

     temperatureReading: { 
     columnName: 'temperatureReading', 
     type: 'string', 
     max: 150, 
     required: true 
     }, 

     dateRecorded: { 
     columnName: 'dateRecorded', 
     type: 'string', 
     }, 
    } 
}; 

routes.js:

module.exports.routes = { 
    'get /enterSensorReading': 'MainController.getSensorReading' 
}; 

MainController.getSensorReading:

getSensorReading: function (request, response) { 

    var temperatureReading = request.param('temperatureReading'); 
    var date = new Date(); 
    var dateRecorded = date.getDate() + "/" 
        + (date.getMonth()+1) + "/" 
        + date.getFullYear() + " " 
        + date.getHours() + ":" 
        + date.getMinutes() + ":" 
        + date.getSeconds(); 

    console.log(temperatureReading); 

    TemperatureReading.create({temperatureReading: temperatureReading, dateRecorded: dateRecorded}).exec(function(err, createdReading) { 
     if(err) { 
      response.send(err); 
     } else { 
      console.log('Created reading'); 
     } 
    }); 

} 

connections.js

module.exports.connections = { 
     mainDatabase: { 
     adapter: 'sails-mysql', 
     host: '127.0.0.1', 
     port: 3306, 
     user: 'myUser', 
     password: 'myPW', 
     database: 'MAIN' 
    } 
}; 

最后,我的数据库结构:

TemperatureReading 
------------------ 
id int(5) PK 
deviceId int(5) 
temperatureReading varchar(255) 
date varchar(255) 

任何想法是什么问题呢?

+0

莫非你的问题添加'temperatureReading'的样本? – myusuf 2014-12-02 09:57:06

+0

您是否也可以在控制台中记录错误。有时候,控制台日志会提供更多的信息,与响应中发送的错误对象相比 – 2014-12-02 09:57:41

回答

1

数据库结构应该是这样的:

mysql> describe TemperatureReading; 
+--------------------+--------------+------+-----+---------+----------------+ 

    | Field    | Type   | Null | Key | Default | Extra   | 
    +--------------------+--------------+------+-----+---------+----------------+ 
    | id     | int(11)  | NO | PRI | NULL | auto_increment | 
    | deviceId   | int(11)  | YES |  | NULL |    | 
    | temperatureReading | varchar(255) | YES |  | NULL |    | 
    | dateRecorded  | varchar(255) | YES |  | NULL |    | 
    | createdAt   | datetime  | YES |  | NULL |    | 
    | updatedAt   | datetime  | YES |  | NULL |    | 
    +--------------------+--------------+------+-----+---------+----------------+ 
    6 rows in set (0.00 sec) 

应该dateRecorded并不仅仅是日期

如果它是一个新的应用程序,你是从头开始创建表,在您的模型中使用add "migrate": "drop"。这会自动为你创建表格。确保在下次提起服务器时删除此行。如果这不能解决问题,那么我认为代码没有任何问题。我测试过,它在我的系统上完美运行。这可能会让你的sails-mysql适配器搞砸了。如果固定你的数据库结构不解决问题,试试这个

npm uninstall sails-mysql 
npm cache clear 
npm install sails-mysql 

更严格的方法是:

rm -Rf node_modules 
npm cache clear 
npm install 

让我知道如果这能解决你面临