Unable to retrieve specific user data. On save, I get a user object and an array of IDs. Saving to the database occurs without problems, but upon receipt it does not return all the columns (the columns userId and assetId do not appear in the output). More specifically, to describe the result of the issue, it gives all the data for all users instead of a specific user. Tell me how to correctly implement the request so that the user receives his data (all columns) and not the data of all users.
------ controller.ts ------------------
@Controller()
export class UserAssetsController {
constructor(private readonly assetsUsersService: UserAssetsService) {}
@UseGuards(JwtAuthGuard)
@Post()
async saveUserAssetsData(@Body() dto: UserAssetCreateDto, @Request() request: any) {
const user = request.user
return this.assetsUsersService.saveUserAssets(user, dto)
}
@UseGuards(JwtAuthGuard)
@Get()
async getUserAssetsData(@Request() request: any) {
return this.assetsUsersService.getUserAssets(request.user)
}
}
-------- service.ts ----------------------
@Injectable()
export class UserAssetsService {
constructor(
@InjectRepository(UserAsset) private readonly repository: BaseRepository<UserAsset>,
@InjectRepository(Asset) private readonly assetRepository: BaseRepository<Asset>,
private readonly cache: CacheService
) {}
async saveUserAssets(user, { assetIds, orderOnPage }: UserAssetCreateDto) {
const userAssets: Array<UserAsset> = []
for (let i = 0; i < assetIds.length; i++) {
const assets = await this.assetRepository.find({ where: [{ id: assetIds[i] }] })
for (const assetIndex in assets) {
const userAsset = this.repository.create({
user,
orderOnPage,
})
userAsset.asset = assets[assetIndex]
await this.repository.save(userAsset)
userAssets.push(userAsset)
}
}
console.log(userAssets)
return userAssets
}
async getUserAssets(user) {
return this.repository.find()
}
}
---------- model.ts -----------------------------
@Entity('user_assets')
export class UserAsset extends TimestampMixin {
@ManyToOne(type => User, user => user.assets, { onDelete: 'CASCADE' })
user: User
@ManyToOne(type => Asset, { onDelete: 'CASCADE' })
asset: Asset
@Column({ nullable: true, length: 250 })
offlineAssetCustomName: string
@Column({ nullable: true })
offlineAssetAnnualRate: number
@Column()
orderOnPage: number
@OneToMany(type => UserAssetComment, comment => comment.asset)
comments: UserAssetComment[]
@OneToMany(type => UserAssetAlert, userAssetAlert => userAssetAlert.asset)
alerts: UserAssetAlert[]
}