node.js - Sequelize Giving 错误 - if (dialectTypes[type.key]) { ^ TypeError : Cannot read property 'key' of null

标签 node.js types orm sequelize.js

我的代码看起来一切都很好。有一个客户表,地址是外部表。但我收到一个错误,说 if (dialectTypes[type.key]) {
类型错误:无法读取 null
的属性“key”。这是我的代码-

module.exports = (sequelize,DataTypes) => {

    const customers = sequelize.define("customers",{
        customerId: {
            type: DataTypes.INTEGER,
            primarykey : true,
            unique: true
        },
        firstname :{
           type: DataTypes.STRING,
           allowNull : false
        },
            
        lastname: DataTypes.STRING,
        username : DataTypes.STRING,
        email: DataTypes.STRING,
        password: DataTypes.STRING,
        mobileNumber: DataTypes.STRING,
        customerGroupId: DataTypes.INTEGER,
        avatar: DataTypes.STRING,
        avatarPath: {
            type: DataTypes.STRING,
            default: "/customer"
        },
        newsLetter: DataTypes.STRING,
        deleteFlag: DataTypes.STRING,
        mailStatus: DataTypes.INTEGER,
        status: DataTypes.INTEGER,
        oauthData: null,
        isActive: DataTypes.INTEGER,
        createdBy: null,
        modifiedBy: null

    })
    return customers;
}
这是地址表 -
 module.exports = ( sequelize,DataTypes) => {

     const address = sequelize.define("address",{
         customerId: DataTypes.INTEGER,
         address : DataTypes.STRING,
         countryId : DataTypes.INTEGER,
         zoneId :DataTypes.INTEGER,
         city: DataTypes.STRING,
         local: DataTypes.STRING,
         pincode : DataTypes.INTEGER 
        })
        return address;
 }
这是我的 index.js 文件,我在其中关联了客户和地址表 -
const {Sequelize,DataTypes} = require('sequelize')

const sequelize = new Sequelize('spurtcommerce','root','',{
    host:'localhost',
    dialect:'mysql',
    port:3308
});

sequelize.authenticate()
.then(()=>{
    console.log("Connection established")
}
)
.catch(error =>{
    console.log("Error connecting to db")
})

const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;

// for customer table--->
db.customers = require('./customer') (sequelize,DataTypes);
db.address = require('./address') (sequelize,DataTypes);

db.sequelize.sync({force:false})
.then(() =>{
    console.log("yes re sync customer")
})
.catch(error=>{
    console.log("error",+error)
})


db.customers.hasOne(db.address,{foreignKey:'customerId'})
db.address.belongsTo(db.customers,{foreignKey:'customerId'})

module.exports = db; 
这是终端——
enter image description here

最佳答案

我直接在 model 中给出了 'null' 值。应该是这样——

 oauthData: {
            type : DataTypes.STRING,
            allowNull : true,
            default: null
        },
        isActive: DataTypes.INTEGER,
        createdBy:{
            type : DataTypes.STRING,
            allowNull : true,
            default: null
        },
        modifiedBy:{
            type : DataTypes.STRING,
            allowNull : true,
            default: null
        },

关于node.js - Sequelize Giving 错误 - if (dialectTypes[type.key]) { ^ TypeError : Cannot read property 'key' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67470355/

相关文章:

java - 如何序列化一组枚举?

node.js - Grunt : Watch multiple files, 仅编译已更改

javascript - 在 Node-webkit 中将 Canvas 保存到磁盘

javascript - node-webkit 和 mysql 查询

javascript - Gulp - 从 src() 获取文件名

json - 转换为字符串后发现对象类型

python - 为什么python -c "print float(7/3)"打印出2.0

haskell - 推断 lambda 表达式的类型

database-design - 在数据库设计中分离电视剧实体

php - 如何保存 Doctrine2 实体