typescript 使用TypeOrm在Nest中保存新实体时出错,Postgresql

mrphzbgm  于 2023-10-22  发布在  TypeScript
关注(0)|答案(1)|浏览(135)

在此错误之前,我只允许一个用户聊天,它工作。现在,即使我尝试只放一个用户,仍然有错误.
即使备份给我同样的错误实体:

import {Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn} from "typeorm";
import {User} from "./user.entity";
import {Order} from "./order.entity";

@Entity()
export class Chat {
    @PrimaryGeneratedColumn()
    id: number;

    @ManyToMany(() => User)
    @JoinTable()
    users: User[];

    @OneToOne(() => Order, (order) => order.chat, {nullable: true})
    order: Order
}

服务内容:

async createChat(userId, employeeId, orderId) {
        try {
            const candidateUser = await this.user.findOne({
                where: {
                    id: userId,
                },
            });

            const candidateEmployee = await this.user.findOne({
                where: {
                    id: employeeId,
                },
            });

            const candidateOrder = await this.order.findOne({
                where: {
                    id: orderId,
                },
            });

            if (candidateOrder && candidateUser && candidateEmployee) {
                const newChat = new Chat();
                newChat.users = [candidateEmployee];
                newChat.order = candidateOrder;

                const savedChat = await this.chat.save(newChat);

                candidateUser.currentChat = savedChat.id;
                candidateEmployee.currentChat = savedChat.id;

                await this.user.save(candidateUser);
                await this.user.save(candidateEmployee);

                candidateOrder.status = 'pending';

                await this.order.save(candidateOrder);

                return savedChat;
            } else {
                throw new Error('Не удалось создать чат.');
            }
        } catch (e) {
            throw new Error(e);
        }
    }

错误:[Nest] 25302 - 07.10.2023,17:27:24错误[ExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExceptionsExc无法执行更新查询,因为未定义更新值。调用"qb. set(...)"方法指定更新的值。错误:UpdateValuesMissingError:无法执行更新查询,因为未定义更新值。调用"qb. set(...)"方法指定更新的值。在ChatService.聊天(/mnt/hdd/web projects/telegram-monolith/telegram-server/src/chat/chat. service. ts:64:19)在processTicksAndRejections(node:internal/process/task_queues:95:5)在/mnt/hdd/web projects/telegram-monolith/telegram-server/node_modules/@nestjs/core/router/router-execution-context. js:46:28在/mnt/hdd/web projects/telegram-monolith/telegram-server/node_modules/@nestjs/core/router/router-proxy. js:9:17

pftdvrlh

pftdvrlh1#

您应该在order中添加@JoinColumn

@Entity()
export class Chat {
    @PrimaryGeneratedColumn()
    id: number;

    @ManyToMany(() => User)
    @JoinTable()
    users: User[];

    @OneToOne(() => Order, (order) => order.chat, {nullable: true})
    @JoinColumn()
    order: Order
}

相关问题