-
Hi, everyone! I am new with Loopback 4 and I am having an issue with implementing the I have the uploads model, which should contain an array of user ids (of the users the file is shared with) that's why I am trying to use the
I am not sure where in my code is wrong. I just followed the example here. Attaching all my model and repository files here. I hope someone can help. Thank you! Upload Model import {belongsTo, Entity, model, property, referencesMany} from '@loopback/repository';
import {User} from './user.model';
@model()
export class Upload extends Entity {
@property({
type: 'string',
id: true,
required: true,
defaultFn: 'uuidv4',
})
id: string;
@property({
type: 'string',
required: true,
})
filename: string;
@property({
type: 'string',
required: true,
})
description: string;
@property({
type: 'string',
required: true,
})
filepath: string;
@belongsTo(() => User)
userId: string;
@referencesMany(() => User)
userIds?: string[];
constructor(data?: Partial<Upload>) {
super(data);
}
}
export interface UploadRelations {
user: User;
users?: User[];
}
export type UploadWithRelations = Upload & UploadRelations; User Model import {Entity, hasMany, model, property} from '@loopback/repository';
import {Chat} from './chat.model';
import {Upload} from './upload.model';
@model()
export class User extends Entity {
@property({
type: 'string',
id: true,
required: true,
defaultFn: 'uuidv4',
})
id: string;
@property({
type: 'string',
required: true,
})
fullname: string;
@property({
type: 'string',
required: true,
})
email: string;
@property({
type: 'string',
required: true,
})
password: string;
@hasMany(() => Chat)
chats?: Chat[];
@hasMany(() => Upload)
uploads?: Upload[];
@hasMany(() => Upload)
sharedUploads?: Upload[];
constructor(data?: Partial<User>) {
super(data);
}
}
export interface UserRelations {
// describe navigational properties here
}
export type UserWithRelations = User & UserRelations; Upload Repository import {Getter, inject} from '@loopback/core';
import {BelongsToAccessor, DefaultCrudRepository, ReferencesManyAccessor, repository} from '@loopback/repository';
import {MongodbDataSource} from '../datasources';
import {Upload, UploadRelations, User} from '../models';
import {UserRepository} from './user.repository';
export class UploadRepository extends DefaultCrudRepository<
Upload,
typeof Upload.prototype.id,
UploadRelations
> {
public readonly users: ReferencesManyAccessor<User, typeof Upload.prototype.id>;
public readonly user: BelongsToAccessor<User, typeof Upload.prototype.id>;
constructor(
@inject('datasources.mongodb') dataSource: MongodbDataSource,
@repository.getter('UserRepository')
usersRepositoryGetter: Getter<UserRepository>,
@repository.getter('UserRepository')
userRepositoryGetter: Getter<UserRepository>,
) {
super(Upload, dataSource);
this.users = this.createReferencesManyAccessorFor('users', usersRepositoryGetter);
this.user = this.createBelongsToAccessorFor('user', userRepositoryGetter);
}
} User Repository import {Getter, inject} from '@loopback/core';
import {DefaultCrudRepository, HasManyRepositoryFactory, repository} from '@loopback/repository';
import {MongodbDataSource} from '../datasources';
import {Chat, Upload, User, UserRelations} from '../models';
import {ChatRepository} from './chat.repository';
import {UploadRepository} from './upload.repository';
export class UserRepository extends DefaultCrudRepository<
User,
typeof User.prototype.id,
UserRelations
> {
public readonly chats: HasManyRepositoryFactory<Chat, typeof User.prototype.id>;
public readonly uploads: HasManyRepositoryFactory<Upload, typeof User.prototype.id>;
public readonly sharedUploads: HasManyRepositoryFactory<Upload, typeof User.prototype.id>;
constructor(
@inject('datasources.mongodb') dataSource: MongodbDataSource,
@repository.getter('ChatRepository')
chatRepositoryGetter: Getter<ChatRepository>,
@repository.getter('UploadRepository')
uploadRepositoryGetter: Getter<UploadRepository>,
@repository.getter('UploadRepository')
sharedUploadRepositoryGetter: Getter<UploadRepository>,
) {
super(User, dataSource);
this.chats = this.createHasManyRepositoryFactoryFor('chats', chatRepositoryGetter);
this.uploads = this.createHasManyRepositoryFactoryFor('uploads', uploadRepositoryGetter);
this.sharedUploads = this.createHasManyRepositoryFactoryFor('sharedUploads', sharedUploadRepositoryGetter);
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @darrellbacarro, Same as my other comment LoopBack 4 does not currently implement References* relations, and only the "traditional" relations are implemented at the moment. |
Beta Was this translation helpful? Give feedback.
Hi @darrellbacarro,
Same as my other comment LoopBack 4 does not currently implement References* relations, and only the "traditional" relations are implemented at the moment.