Encountering an issue with retrieving specific user data. When saving, a user object and an array of IDs are obtained. The database saving process is successful; however, upon retrieval, not all columns (specifically userId and assetId) are included in the output. Instead of fetching data for a particular user, it returns data for all users. Any suggestions on how to correctly implement the request so that only the user's data (including all columns) is returned?https://i.sstatic.net/HVCep.png
------ 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[]
}