gpt4 book ai didi

javascript - 我的 Sequelize 关联有困难

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

Before asking this question I have spent days finding a solution thatsolved my problem but no luck. Every question that I search on Googleis not valid. I have searched so much that every link in the result ispink.


嘿大家。我正在努力在 3 个模型之间建立关联:评论、用户和帖子。我正在制作一个简单的网络博客,其中包含帖子下的身份验证帖子和评论。解释将不胜感激。
到目前为止,这是我的模型。
// comment.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Comment extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.belongsTo(models.Post);
}
}
Comment.init(
{
comment: {
type: DataTypes.STRING,
allowNull: false,
},
postId: {
type: DataTypes.INTEGER,
allowNull: false,
},
authorId: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
sequelize,
modelName: "Comment",
}
);
return Comment;
};
// post.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Post extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.hasMany(models.Comment);
}
}
Post.init(
{
title: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.TEXT, allowNull: false },
imageUrl: { type: DataTypes.STRING, allowNull: false },
},
{
sequelize,
modelName: "Post",
}
);
return Post;
};
// user.js
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
}
User.init(
{
email: { type: DataTypes.STRING, allowNull: false },
password: { type: DataTypes.STRING, allowNull: false },
name: { type: DataTypes.STRING, allowNull: false },
},
{
sequelize,
modelName: "User",
}
);
return User;
};
当我尝试获取所有具有相应帖子 ID 的评论时,我在帖子 Controller 中收到此错误
TypeError: include.model.getTableName is not a function
at Function._validateIncludedElement (C:\Users\micha\Desktop\blog\node_modules\sequelize\lib\model.js:579:30)
at C:\Users\micha\Desktop\blog\node_modules\sequelize\lib\model.js:509:37
at Array.map (<anonymous>)
at Function._validateIncludedElements (C:\Users\micha\Desktop\blog\node_modules\sequelize\lib\model.js:504:39)
at Function.findAll (C:\Users\micha\Desktop\blog\node_modules\sequelize\lib\model.js:1723:12)
at async Function.findOne (C:\Users\micha\Desktop\blog\node_modules\sequelize\lib\model.js:1917:12)
at async exports.getPostById (C:\Users\micha\Desktop\blog\src\controllers\post.contoller.js:14:18)
这是发生错误的 Controller 片段
    const post = await Post(sequelize, DataTypes).findOne({
where: {
id: req.params.postId,
},
include: [{ model: Comment, as: "comments" }],
});
提前致谢,也感谢您提供时间。我希望我们能够度过这一切

最佳答案

您可能需要切换查询。您可以在 Post 上找到所有,而不是 Comment 上的 findOne 。像这样

Comment.findAll({
include: [{
model: Post,
where: { id: req.params.postId }
}]
})

关于javascript - 我的 Sequelize 关联有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65993972/

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