我在续集关联方面遇到困难

I'm having difficulties with my sequelize associations

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

大家好。我正在努力在 3 个模型之间创建关联:评论、用户和 Post。我正在制作一个简单的网络博客,其中包含 post 身份验证和 post 下的评论。解释将不胜感激。

这是我目前的模型。

// 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;
};

当我试图获取所有具有相应 post Id

的评论时,我的 post 控制器出现此错误
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)

这是发生错误的控制器片段

    const post = await Post(sequelize, DataTypes).findOne({
      where: {
        id: req.params.postId,
      },
      include: [{ model: Comment, as: "comments" }],
    });

提前致谢,也感谢您抽出宝贵时间。我希望我们能够度过难关

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

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