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

问题描述 投票:0回答:1

我刚刚开始学习 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 GroupMembersModelAttributes”类型的参数。

输入 '{ id: 数字; }' 不可分配给类型 'WhereOptions |不明确的'。对象字面量只能指定已知属性,并且“WhereOptions”类型中不存在“id”。

javascript node.js typescript model-view-controller sequelize.js
1个回答
0
投票

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

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

const userGroups = await GroupMembers.findAll({
  where: { user_id },
  include: [{ model: Groups, as: 'Groups' }],
});
© www.soinside.com 2019 - 2024. All rights reserved.