首页 > 解决方案 > 我的续集关联有困难

问题描述

在问这个问题之前,我花了几天时间寻找解决我的问题但没有运气的解决方案。我在 Google 上搜索的每个问题都是无效的。我搜索了很多,结果中的每个链接都是粉红色的。

嘿大家。我正在努力在 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 的所有评论时,我的帖子控制器中出现此错误

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" }],
    });

在此先感谢您,也感谢您提供时间。我希望我们能够度过这个难关

标签: javascriptnode.jssequelize.js

解决方案


您可能需要切换查询。Post您可以使用 findAll on代替 findOne on Comment。像这样

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

推荐阅读