0

I am learning Adonis Js v5 Framework. I trying to make mixin for User Model as documented here: click

Below I have attached code and Image of error that I am getting

I just want to know Am I doing it in right way or is there any other workaround for this

This is mixin HasProfilePhoto.ts


import { BaseModel} from "@ioc:Adonis/Lucid/Orm";
import { NormalizeConstructor } from '@ioc:Adonis/Core/Helpers'


const HasProfilePhoto = <T extends NormalizeConstructor<typeof BaseModel>>(superclass:T)=>{

    return class extends superclass{

        public async updateProfilePhoto(photo:any):Promise<void>{
            await photo.moveToDisk('./');
            //@ts-ignore
            this.profilePhotoPath = photo.fileName;
            await this.save();

        }

        public defaultProfilePhotoUrl():string
        {
            //@ts-ignore
            let name = this.name.split(' ').map(segment=>{
                return segment.substring(0,1);
            }).join(' ').trim();

            return `https://ui-avatars.com/api/?name=${encodeURIComponent(name)}&color=7F9CF5&background=EBF4FF`;
        }
    }
}

export default HasProfilePhoto;

This is UserModel File

import { DateTime } from 'luxon'
import Hash from '@ioc:Adonis/Core/Hash'
import { column, beforeSave, BaseModel, computed, afterFind } from '@ioc:Adonis/Lucid/Orm'
import { compose } from '@ioc:Adonis/Core/Helpers';
import HasProfilePhoto from 'App/Mixins/HasProfilePhoto';
import HasApiTokens from 'App/Mixins/HasApiTokens';
import Drive from '@ioc:Adonis/Core/Drive'
import Env from '@ioc:Adonis/Core/Env';


export default class User extends compose(BaseModel,HasApiTokens,HasProfilePhoto) {

    @column({ isPrimary: true })
    public id: number

    @column()
    public name: string

    @column()
    public email: string

    @column({ serializeAs: null })
    public password: string

    @column()
    public rememberMeToken: string | null

    @column()
    public profilePhotoPath: string | null

    @computed({serializeAs:'profile_photo_url'})
    public profilePhotoUrl: string;

    @column.dateTime()
    public emailVerifiedAt:DateTime|null

    @column.dateTime({ autoCreate: true })
    public createdAt: DateTime

    @column.dateTime({ autoCreate: true, autoUpdate: true })
    public updatedAt: DateTime


    @beforeSave()
    public static async hashPassword (user: User) {
        if (user.$dirty.password) {
        user.password = await Hash.make(user.password)
        }
    }

    @afterFind()
    public static async addPofilePhotoUrl(user:User){
        
        if(user.profilePhotoPath){
            let url = await Drive.getUrl(user.profilePhotoPath);
            url = Env.get('APP_URL')+url
            //this.$addColumn('profile_photo_url',{});
            user.profilePhotoUrl = url;
            return;
        }
        user.profilePhotoUrl = user.defaultProfilePhotoUrl();
    }




}


IMAGE

0 Answers0