我已经在我的项目中安装了 mysql sequelize 然后我使用创建模型和迁移文件的 sequelize 命令创建了消息模型。我还在 config.json 文件中给出了数据库凭据。现在我正在尝试在我的数据库中插入记录,但在这里收到此错误是我的模型文件
我是否需要明确地建立数据库连接,或者它会自动从 config.json 文件中选择凭据来建立连接?
模型/消息.js
'use strict';
module.exports = (sequelize, DataTypes) => {
var Messages = sequelize.define('Messages', {
room: DataTypes.STRING,
nickname: DataTypes.STRING,
message: DataTypes.STRING,
receiver: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Messages;
};
这是 config.json 文件 config/config.json
{
"development": {
"username": "root",
"password": "asad",
"database": "chat",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
这是我保存记录 app.js 文件的服务器文件
var express = require('express');
var router = express.Router();
var app = express();
var server = require('http').createServer(app);
server.listen(4000);
var Messages=require('../models/Messages.js');
router.post('/', function(req, res, next) {
var a =req.body;
Messages.create(a).then(() => {
console.log('success ')
})
})