错误:字段classMethods的无法识别的数据类型

时间:2017-02-17 05:19:52

标签: javascript database sequelize.js

使用以下模型定义时:

var address = connection.define('address', {
    id : {
        type :sequelize.INTEGER ,
        primaryKey : true ,
        autoIncrement : true
    },
    content : sequelize.TEXT ,
    classMethods : {
        associate : function(model){
            var user  = model.user; 
            user.hasMany(address);
        }
    }
});

我无法调试:Error: Unrecognized data type for field classMethods

3 个答案:

答案 0 :(得分:2)

显然,您已将模型定义部分与其他选项部分混合在一起。看看像Sequelize classMethods vs instanceMethods这样的例子,它们应该是两个对象,作为两个单独的参数。

因此,您的代码至少应修改为:

var address = connection.define('address', {
    id: {
        type: sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    content: sequelize.TEXT
},
// <-- here, another object / argument started
{
    classMethods: {
        associate: function (model) {
            var user = model.user;
            user.hasMany(address);
        }
    }
});

答案 1 :(得分:1)

我错过了支架并面对这个问题

'use stri

ct';

module.exports = function(sequelize, DataTypes) {
    var category = sequelize.define('category', {
        name: DataTypes.STRING,
        image_url: DataTypes.STRING,
        thumb_url: DataTypes.STRING,
        type1: {
            type: DataTypes.ENUM,
            values: ['public_services', 'local_services']
        }
    }//this one, 



{//this bracket
            classMethods: {
                associate: function(models) {
                    // associations can be defined here
                    //   category.hasMany(models.directory, [{foreignKey:'dir_fid'}])
                }
            }
        });
        return category;
    };

答案 2 :(得分:0)

var address = connection.define('address',{
    id:{
        type :sequelize.INTEGER ,
        primaryKey : true ,
        autoIncrement : true
    },
    content : sequelize.TEXT
    },
    {
        classMethods : {
            associate : function(model){
                var user  = model.user; 
                user.hasMany(address);
            }
        }
    }

);

^^语法问题