gpt4 book ai didi

javascript - 在 Model.belongsTo 调用不是 Sequelize.Model 实例的东西

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:30 25 4
gpt4 key购买 nike

我试图在两个模型之间引用外键。

但是我收到了这个错误:

throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not an instance of Sequelize.Model');
called with something that's not an instance of Sequelize.Model
at Model.belongsTo

我该如何解决这个问题?

到目前为止,这是我的代码。

这是我的模型/mercadolibre.js

"use strict";
var User = require('../models/index').User;

module.exports = function(sequelize, DataTypes) {
var MercadoLibre = sequelize.define("MercadoLibre", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
access_token: DataTypes.STRING,
refresh_token: DataTypes.STRING,
environment_hash: DataTypes.STRING
}, {
tableName: 'mercadolibres',
underscored: true,
timestamps: true
}

);

MercadoLibre.belongsTo(User);

return MercadoLibre;
};

这是我的模型/user.js

"use strict";

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.STRING,
slack_id: DataTypes.STRING,
environment_hash: {
type: DataTypes.STRING,
defaultValue: DataTypes.UUIDV4
}
}, {
tableName: 'users',
underscored: false,
timestamps: false
}

);

return User;
};

这是我的模型/index.js

"use strict";

var fs = require("fs");
var path = require("path");
var Sequelize = require('sequelize')
, sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
port: 3306, // or 5432 (for postgres)
});


var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

最佳答案

像这样使用:

module.exports = function(sequelize, DataTypes) {
var MercadoLibre = sequelize.define("MercadoLibre", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
access_token: DataTypes.STRING,
refresh_token: DataTypes.STRING,
environment_hash: DataTypes.STRING
}, {
tableName: 'mercadolibres',
underscored: true,
timestamps: true,
classMethods: {
associate : function(models) {
MercadoLibre.belongsTo(models.User)
},
},
});



return MercadoLibre;

};

关于javascript - 在 Model.belongsTo 调用不是 Sequelize.Model 实例的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com