TypeError:无法读取Function.ADataMembers.associate上未定义的属性“ hasOne”。这是ADataMember.js
'use strict'
module.exports = (sequelize, DataTypes) => {
var ADataMembers = sequelize.define('data_members', {
code: {
type: DataTypes.STRING,
primaryKey: true
},
names: {
type: DataTypes.STRING,
allowNull: false
},
first_last_name: {
type: DataTypes.STRING,
allowNull: false
},
second_last_name: {
type: DataTypes.STRING,
allowNull: false
},
career_id: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING
},
phone: {
type: DataTypes.STRING
},
image_url: {
type: DataTypes.TEXT
},
name_status: {
type: DataTypes.INTEGER
},
has_key: {
type: DataTypes.BOOLEAN
},
birthday: {
type: DataTypes.DATE
}
})
ADataMembers.associate = function(models) {
models.ADataMembers.hasOne(models.ACareers)
models.ADataMembers.hasOne(models.AMemberStatus)
models.ADataMembers.belongsTo(models.ADataArea, {
foreignKey: {
allowNull: false
}
})
}
return ADataMembers
}
这是我的index.js
'use strict'
const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const basename = path.basename(__filename)
const env = process.env.NODE_ENV || 'development'
const config = require(__dirname + '/../config/config.json')[env]
const db = {}
let sequelize
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config)
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js')
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file))
db[model.name] = model
})
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db)
}
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
我一直在关注此示例https://sequelize.readthedocs.io/en/1.7.0/articles/express/#introduction 但是出了点问题。如果有人可以帮助我,我将不胜感激。
答案 0 :(得分:0)
您要使用其他名称进行定义。
更改此:
var ADataMembers = sequelize.define('data_members', {
// ^ this is the problem
对此:
var ADataMembers = sequelize.define('ADataMembers', {