NodeJS TypeScript Sequelize:尝试在查询中包含相关模型时出错

um6iljoc  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(136)

我刚刚开始学习NodeJs,我已经在NodeJs中创建了我的第一个项目,现在我正试图将其迁移到Typescript中,但我无法删除此错误。有人能帮我修一下吗?
这是我的group.service.ts文件。

import { Model, DataTypes, Sequelize } from 'sequelize';
import GroupMembers from '../models/groupmember.model';
// Import Sequelize instance (assuming it's defined in a different file)
import { sequelize } from '../models'; // Adjust the import path to match your project structure
import Groups from '../models/group.model';
class Group extends Model {
  public id!: number;
  public group_Name!: string;
}

Group.init(
  {
    group_Name: {
      type: DataTypes.STRING, // Adjust the data type as needed
      allowNull: false,
    },
  },
  {
    sequelize,
    modelName: 'Groups',
  }
);

class GroupMember extends Model {
  public id!: number;
  public user_id!: number;
  public group_id!: number;
}

GroupMember.init(
  {
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    group_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
  },
  {
    sequelize,
    modelName: 'GroupMembers',
  }
);

// Function to create a new group
const createGroup = async (group_Name: string, creator_id: number): Promise<Groups> => {
  try {
    const group = await Groups.create({
      creator_id,
      group_Name,
    });
    return group;
  } catch (error) {
    console.error('Error in Creating a group', error);
    throw new Error('Error in creating a group');
  }
};

// Function to get all the groups a user is part of, including private chats
const getUsersGroups = async (user_id: number): Promise<any[]> => {
  try {
    const userGroups = await GroupMembers.findAll({
      where: { user_id },
      include: [{ model: Groups }],
    });
    console.log("checking grouos", userGroups);
    return userGroups.map((entry) => entry.getDataValue('groups'));
  } catch (error) {
    console.error('Error in getting groups of the user', error);
    throw new Error('Error in getting groups of the user');
  }
};

// Function to delete a group along with its associated messages
const deleteGroupWithMessages = async (group_id: number): Promise<{ message: string }> => {
  try {
    const deleteGroup = await Groups.destroy({
      where: { id: group_id },
    });

    if (!deleteGroup) {
      throw new Error('Group not found');
    }

    return { message: 'Group and associated messages deleted successfully' };
  } catch (error) {
    console.error('Error in deleting group and group messages', error);
    throw new Error('Error in deleting group and group messages');
  }
};

export { createGroup, getUsersGroups, deleteGroupWithMessages };

这些是我遇到的问题
"“groups”“类型的参数不能分配给”“keyof GroupControllerModelAttributes”“类型的参数。
类型'{ id:number;}“不能分配给类型”WhereOptions|未定义'。对象文本只能指定已知属性,并且“id”在类型“WhereOptions”中不存在。

w8rqjzmb

w8rqjzmb1#

首先,我们改变了这一点,因为TypeScript无法找到属性组。若要解决此问题,可以利用GroupMembers类型定义并使用其属性而不是groups字符串。

return userGroups.map((entry) => entry.Groups);

const userGroups = await GroupMembers.findAll({
  where: { user_id },
  include: [{ model: Groups, as: 'Groups' }],
});

相关问题