【发布时间】:2019-03-06 03:13:12
【问题描述】:
我正在尝试使用 Sequelize 和 mysql db 创建一个模型。我正在尝试发布到“/students/register”,它一直给我一个错误,说 findOne 不是一个函数。我尝试要求我的 sql 但它不工作..我还尝试了一个不同的函数,如 findAll 并且仍然不工作。似乎是什么问题。谢谢
models/Students.js
const Sequelize = require('sequelize');
const sequelize = require('../database/db')
module.exports = (sequelize, Sequelize) => {
const Student = sequelize.define(
'student', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
created: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
}
}, {
timestamps: false
});
module.exports = Student;
};
数据库/db.js
const Sequelize = require('sequelize')
const sequelize = new Sequelize('canavs', 'root', 'root', {
host: 'localhost',
port: 8889,
dialect: 'mysql',
operatorAliases: false,
pool: {
max: 5,
min: 0,
acquire: 3000,
idle: 10000
}
})
// sequelize.import('./models/Students')
module.exports = sequelize;
index.js
const Student_Info = require('./models/Students')
const db = require('./database/db')
// app.use('/students', Student)
app.get('/getName', (req, res) => {
Student_Info.findOne({
where: {
id: 1
}
})
.then((student) => {
res.json(student.name);
})
.catch((err) => {
res.send('error' + err)
}
)
})
【问题讨论】:
标签: mysql node.js express sequelize.js