gpt4 book ai didi

mysql - Sequelize N :M ParentModel. setChilds(ChildArray) 不是函数(魔术方法?)

转载 作者:行者123 更新时间:2023-12-03 22:32:31 27 4
gpt4 key购买 nike

在我的程序中,我想更新许多具有名为 Column 的父表的类别。此外,一个类别可以包含许多列。 (N:M)
所以我尝试更新帖子的类别,但失败了。这是我试过的代码

let updateColumn = async function(req, res) {
let column_id = req.body.id;
let column_title = req.body.title;
let column_content_text = req.body.contentText;
let column_content_html = req.body.contentHTML;
let column_categorys = req.body.categoris;
let column_tags = req.body.tags;

let column_thumbnail_url = process.env.CDN_COLUMN_ENDPOINT;
column_thumbnail_url += column_title;
column_thumbnail_url += '/';
column_thumbnail_url += '0.png';

var category_array = [];
var tag_array = [];

await sequelize.transaction(async transaction => {
try {
await Column.update({
column_title: column_title,
column_content_text: column_content_text,
column_content_html: column_content_html,
column_thumbnail_url: column_thumbnail_url,
}, {
where: {
id: column_id,
},
}, {transaction: transaction});

const column = await Column.findOne({
where: {
id: column_id,
},
transaction,
});

for(var i in column_categorys) {
const [category, created] = await Category.findOrCreate({
where : {
category_name: column_categorys[i],
},
transaction,
});

category_array.push(category);
}

for(var j in column_tags) {
const [tag, created] = await Tag.findOrCreate({
where : {
tag_name: column_tags[j],
},
transaction,
});

tag_array.push(tag);
}

await column.setCategorys(
category_array,
{ transaction },
);

await column.setTags(
tag_array,
{ transaction },
);

res.json({
responseCode: 400,
responseMsg: 'Success',
});
} catch (err) {
console.error(err);
res.json({
responseCode: 404,
responseMsg: 'Fail',
});
}
});
}
column_info.js
const Sequelize = require('sequelize');

module.exports = class Column extends Sequelize.Model {
static init(sequelize) {
return super.init({
column_title: {
type: Sequelize.STRING(200),
allowNull: false,
unique: true,
},
column_content_text: {
type: Sequelize.STRING(10000),
allowNull: false,
},
column_content_html: {
type: Sequelize.STRING(10000),
},
column_thumbnail_url: {
type: Sequelize.STRING(300),
},
}, {
sequelize,
timestamps: true,
underscored: true,
modelName: 'Column',
tableName: 'columns',
paranoid: true,
charset: 'utf8',
collate: 'utf8_general_ci',
});
}

static associate(db) {
db.Column.belongsToMany(db.Category, {
through: 'ColumnCategory',
unique: false,
});
db.Column.belongsToMany(db.Tag, {
through: 'ColumnTag',
unique: false,
});
}
};
category_info.js
const Sequelize = require('sequelize');

module.exports = class Category extends Sequelize.Model {
static init(sequelize) {
return super.init({
category_name: {
type: Sequelize.STRING(50),
allowNull: false,
unique: true,
},
}, {
sequelize,
timestamps: false,
underscored: true,
modelName: 'Category',
tableName: 'categorys',
paranoid: false,
charset: 'utf8',
collate: 'utf8_general_ci',
});
}

static associate(db) {
db.Category.belongsToMany(db.Column, {
through: 'ColumnCategory',
unique: false,
});
}
};
模型/index.js
const Sequelize = require('sequelize');
const User = require('./user');
const Baby = require('./baby_info');
const UserCase = require('./user_case');
const Column = require('./column_info');
const Tag = require('./tag_info');
const Category = require('./category_info');

const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config')[env];
const db = {};

const sequelize = new Sequelize(config.database, config.username, config.password, config);

db.sequelize = sequelize;

db.User = User;
db.Baby = Baby;
db.UserCase = UserCase;
db.Column = Column;
db.Tag = Tag;
db.Category = Category;

User.init(sequelize);
Baby.init(sequelize);
UserCase.init(sequelize);
Column.init(sequelize);
Tag.init(sequelize);
Category.init(sequelize);

User.associate(db);
Baby.associate(db);
UserCase.associate(db);
Column.associate(db);
Tag.associate(db);
Category.associate(db);

module.exports = db;
错误日志
0|app      | 21-01-04 16:09:26: TypeError: column.setCategorys is not a function
0|app | 21-01-04 16:09:26: at /home/ubuntu/bebenity/routes/column/column.ctrl.js:66:20
0|app | 21-01-04 16:09:26: at processTicksAndRejections (internal/process/task_queues.js:93:5)
0|app | 21-01-04 16:09:26: at async /home/ubuntu/bebenity/node_modules/sequelize/lib/sequelize.js:1090:24
0|app | 21-01-04 16:09:26: at async updateColumn (/home/ubuntu/bebenity/routes/column/column.ctrl.js:21:3)

你能告诉我这段代码的哪一部分我错误地使用了关系查询吗?

添加 1
其实我也有过类似的经历。
How to add child row in related table?? (Sequelzie N:M Relationship)
在那个例子中,方法 findOrCreate 的回调值有问题。
但是,在这个问题中,方法 findOne 的回调值显然是 模型 对象!我已经通过 column instanceof Column 检查过,该列是 Column 模型实例。(确实如此)
但是在模型与 Create 方法和 findOne 之间有点不同
创建回调
enter image description here
findOne回调
enter image description here

最佳答案

解决方案非常简单......
将我的 column.setCategorys 修改为 column.setCategories 是...
我的天啊

关于mysql - Sequelize N :M ParentModel. setChilds(ChildArray) 不是函数(魔术方法?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65566013/

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