Message.Create 不是函数 sequelize cli
Message.Create is not a function sequelize cli
我已经在我的项目中安装了 mysql sequelize 然后我使用创建模型和迁移文件的 sequelize 命令创建了消息模型。我还在 config.json file.Now 中提供了数据库凭据我我试图在我的数据库中插入记录,但这里出现此错误是我的模型文件
DO i need to make db connection explicitly or it auto picks the
credential from config.json file to make connection ?
models/messages.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 ')
})
})
制作sequelize连接实例并导出,请根据您的环境使用配置
const sql = new Sequelize(config.database, config.username, config.password, {
host: config.host,
dialect: config.dialect,
pool: {
max: 5,
min: 0,
idle: 10000
},
});
module.export = sql;
在您的 app.js 中导入 sql 实例并在启动服务器之前连接您的数据库。
var express = require('express');
var router = express.Router();
var app = express();
var server = require('http').createServer(app);
var sql = require(./sql);
sql.authenticate().then(()=>{
server.listen(4000);
});
var Messages= sql.import('../models/Messages.js');
router.post('/', function(req, res, next) {
var a =req.body;
Messages.create(a).then(() => {
console.log('success ')
})
})
我已经在我的项目中安装了 mysql sequelize 然后我使用创建模型和迁移文件的 sequelize 命令创建了消息模型。我还在 config.json file.Now 中提供了数据库凭据我我试图在我的数据库中插入记录,但这里出现此错误是我的模型文件
DO i need to make db connection explicitly or it auto picks the credential from config.json file to make connection ?
models/messages.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 ')
})
})
制作sequelize连接实例并导出,请根据您的环境使用配置
const sql = new Sequelize(config.database, config.username, config.password, {
host: config.host,
dialect: config.dialect,
pool: {
max: 5,
min: 0,
idle: 10000
},
});
module.export = sql;
在您的 app.js 中导入 sql 实例并在启动服务器之前连接您的数据库。
var express = require('express');
var router = express.Router();
var app = express();
var server = require('http').createServer(app);
var sql = require(./sql);
sql.authenticate().then(()=>{
server.listen(4000);
});
var Messages= sql.import('../models/Messages.js');
router.post('/', function(req, res, next) {
var a =req.body;
Messages.create(a).then(() => {
console.log('success ')
})
})