Update user photo data with nodejs and Typeorm

Asked

Viewed 249 times

0

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import Doctor from './Doctor';
import Patient from './Patient';

@Entity('images')
export default class Image {
    @PrimaryGeneratedColumn('increment')
    id: number;

    @Column()
    name: string;

    @Column()
    size: number;

    @Column()
    url: string;

    @Column()
    key: string;

    @Column()
    isAvatar: boolean;

    @ManyToOne(() => Doctor, doctor => doctor.images)
    @JoinColumn({name: 'doctorId'})
    doctor: Doctor;

    @ManyToOne(() => Patient, patient => patient.images)
    @JoinColumn({name: 'patientId'})
    patient: Patient;
}

Image Model.

import { Entity, Column, PrimaryGeneratedColumn, OneToMany, JoinColumn } from 'typeorm';
import Image from './Image';

@Entity('doctors')
export default class Doctor {
    @PrimaryGeneratedColumn('increment')
    id: number;

    @Column()
    name: string;

    @Column()
    birthDate: Date;

    @Column()
    email: string;

    @Column()
    cellphone: string;

    @Column()
    crm: string;

    @Column()
    specialty: string;

    @Column()
    about: string;

    @Column()
    city: string;

    @Column()
    consult_price: string;

    @Column()
    password: string;

    @OneToMany(() => Image, image => image.doctor, {
        cascade: ['insert','update']
    })
    @JoinColumn({name:'doctorId'})
    images: Image[];
}

Model of the Doctor


    async updateProfile(req: Request,res: Response) {

        let requestImages = req.files as Express.Multer.File[];

        const { isAvatar } = req.body;
        
        const images = requestImages.map((image:Img) => {
        
            return { 
                url: process.env.STORAGE_TYPE === 'local' ? `${process.env.APP_URL}/pictures/${image.filename}` : image.location,
                name:image.originalname, 
                size: image.size,
                key: image.filename,
                isAvatar: image.originalname === isAvatar ? true : false
            }
        });

        const { id } = req.params;

        const { 
            name,
            birthDate,
            email,
            cellphone, 
            CRM,
            specialty,
            about,
            city,
            consult_price
        } = req.body;

        const doctorRepository = getRepository(Doctor);

        try {
            const updatedProfile = await doctorRepository.findOneOrFail(id,{
                relations:['images']
            });

            updatedProfile.name = name || updatedProfile.name;
            updatedProfile.birthDate = birthDate || updatedProfile.birthDate;
            updatedProfile.email = email || updatedProfile.email;
            updatedProfile.crm = CRM || updatedProfile.crm;
            updatedProfile.specialty = specialty || updatedProfile.specialty;
            updatedProfile.cellphone = cellphone || updatedProfile.cellphone;
            updatedProfile.about = about || updatedProfile.about;
            updatedProfile.city = city || updatedProfile.city;
            updatedProfile.consult_price = consult_price || updatedProfile.consult_price;
            updatedProfile.images = images;

            await doctorRepository.save(updatedProfile);
            return res.json(updatedProfile);
        } catch {
            return res.status(400).send("Not found");
        }
    }

Code above represents the attempt to update data, all data is being updated successfully, however, I do not know what to do about the images!! The multer and S3 were used. When I try to make the following line of code:

updatedProfile.images = images;

The following error appears:

Type '{ url: string | Undefined; name: string; size: number; key: string; isAvatar: Boolean; }[]' is not Assignable to type 'Image[]'. Type '{ url: string | Undefined; name: string; size: number; key: string; isAvatar: Boolean; }' is Missing the following properties from type 'Image': id, doctor, patientts(2322)

That is, it is necessary that the new images are already in the database but how would do it the way the code was structured?

1 answer

0

This typing error occurs because the images column has been typed as an Image array, and when you are associating using updatedProfile.images = images; you are passing an object that is outside an array.

One way to solve this would be there in your image model, you exchange images: Image[]; for images: Image; if you are going to use a single image.

Or else you can use updatedProfile.images = [images];

  • So I tried that from there, but the error continues, the problem is not being in the array, because the new images is an array, the problem is that this new array contains images that have not yet been saved in the database and, as they have not yet been, have no id or anything like that. There I am trapped trying to save them before but still it is giving error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.