在我的路线文件上运行“ findAll”功能时,始终会出现错误。我已经搜索过,可能是因为我设置了basics.js模型文件夹的方式。
在导出模型之前就可以使用,但是我将模型更改为现在的模型,因为我不断收到错误消息“ define is undefined”
db.js
const Sequelize = require('sequelize')
const sequelize = new Sequelize('database', 'username' , 'password' , {
host: 'localhost',
dialect: 'mysql',
})
const db = {};
db.sequelize = sequelize;
db.Sequelize = Sequelize;
// Models
db.ProUsers = require('../model/User.js')(sequelize, Sequelize);
db.ProPersonals = require('../model/profile_personal.js')(sequelize, Sequelize);
// Foreign Key Connections
db.ProPersonals.belongsTo(db.ProUsers, {foreignKey: 'fk_user_id', targetKey: 'user_id'});
db.ProUsers.hasOne(db.ProPersonals, {foreignKey: 'fk_user_id', targetKey: 'user_id'});
module.exports = db
Basic.js //模型
module.exports = (sequelize, Sequelize) => {
const Profile_Basics = sequelize.define('basics', {
ID: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
resume: {
type: Sequelize.STRING
}
},
{
timestamps: false
});
return Profile_Basics;
}
basics.js //路由器
var express = require("express")
var router = express.Router()
const Basic = require("../model/Basic.js")
// get all jobs
router.get("/", (req, res) => {
Basic.findAll()
.then(basics => {
res.json(basics)
})
.catch(err => {
res.send('error: ' + err)
})
})