I´am trying to call the function
getPost()
as soon as the url is called with an hashtag from the HashtagRepository as param. It should return an image link from the HashtagRepository when called. While testing the URL, my console just throws an error:
Cannot read properties of undefined(reading 'getPost')
I am a beginner and it's my first time working with Nest.JS and Typescript
The HashtagRepository Class:
import { Injectable } from '@nestjs/common'
type HashtaggedMedia = {
image: string
hashtag: string
}
@Injectable()
export class HashtagRepository {
private data: HashtaggedMedia[] = [
{
image: 'https://scontent-frt3-2.cdninstagram.com/v/t51.2885-15/311661367_2148843741987513_3584984416517872912_n.webp?stp=dst-jpg_e35&_nc_ht=scontent-frt3-2.cdninstagram.com&_nc_cat=109&_nc_ohc=sTRqy7BGzYkAX9Hj3Kd&edm=AJ9x6zYBAAAA&ccb=7-5&ig_cache_key=Mjk0NzYxMjEwODY0NzE5NTU4OA==.2-ccb7-5&oh=00_AT8twEU1_NK0d_BiKbFaw7PFP8ihgiPz02pJkCpN2f65GQ&oe=634F577F&_nc_sid=cff2a4',
hashtag: '#FranksBurgers',
},
{
image: 'https://scontent-frx5-1.cdninstagram.com/v/t51.2885-15/311152149_182028907724182_7846213112569143555_n.jpg?stp=dst-jpg_e35&_nc_ht=scontent-frx5-1.cdninstagram.com&_nc_cat=110&_nc_ohc=t6dxbywE69YAX8_vRts&edm=ALQROFkBAAAA&ccb=7-5&ig_cache_key=Mjk0NzMyODk5MDM0NDY0NzEyOQ==.2-ccb7-5&oh=00_AT8YzYRzkppuN9y8kL9cV_k8Z-gmL3PIbsnpCylyDr0x9w&oe=635081E1&_nc_sid=30a2ef',
hashtag: '#FranksBurgers',
},
{
image: 'https://scontent-frt3-1.cdninstagram.com/v/t51.2885-15/251277124_605843170457678_5105607592633822570_n.jpg?stp=dst-jpg_e35&_nc_ht=scontent-frt3-1.cdninstagram.com&_nc_cat=102&_nc_ohc=HXCQ2eywbUAAX8NVghy&edm=ALQROFkBAAAA&ccb=7-5&ig_cache_key=MjY5NzQ0NDY1NTMzNzU2MTEyMw==.2-ccb7-5&oh=00_AT_9YGwuZnByJoXzMyunOdBH1kukXG2KAtsrBU71mMqdsw&oe=6350DCD3&_nc_sid=30a2ef',
hashtag: '#FranksBurgers',
},
{
image: 'https://scontent-frx5-1.cdninstagram.com/v/t51.2885-15/271113427_238607281623214_5291842888525756529_n.jpg?stp=dst-jpg_e35&_nc_ht=scontent-frx5-1.cdninstagram.com&_nc_cat=110&_nc_ohc=lSN2xj3PFfwAX9vnypL&tn=Jrv1_r65tof7Hnsk&edm=ALQROFkBAAAA&ccb=7-5&ig_cache_key=Mjc0MTY3NDI1MzI2NDU3MTUxMw==.2-ccb7-5&oh=00_AT-6rxO1Jd7kR9RQDy3ypDvc6mM1l0RJRbAtbxORP1L7lw&oe=6350CBE8&_nc_sid=30a2ef',
hashtag: '#FranksBurgers',
},
{
image: 'empty',
hashtag: 'franksburgers',
},
]
}
The getPost() function in the HashTagRepositoryClass;
getPost(hashtag: string): Rest.HashtaggedMediaResponse {
const images = this.data
.filter((hashtaggedMedia) => hashtaggedMedia.hashtag===hashtag).map((hashtaggedMedia) => hashtaggedMedia.image)return {
hashtag: hashtag,
images,
}
}
}
The Service class:
import { Injectable } from '@nestjs/common'
import { FollowerCount } from './misc/follower-count.mock'
import { HashtagRepository } from './misc/hashtag-repository.mock'
@Injectable()
export class AppService {
hashtagRepository: HashtagRepository
constructor(
private followerCount: FollowerCount,
private getPost: HashtagRepository,
) {}
async fetchFollowerCount(
profileHandle: string,
): Promise<Rest.FollowerCountResponse> {
return this.followerCount.getCount(profileHandle)
}
async fetchPosts(hashtag: string): Promise<Rest.HashtaggedMediaResponse> {
return this.hashtagRepository.getPost(hashtag)
}
}
The Controller class:
import { BadRequestException, Controller, Get, Param } from '@nestjs/common'
import { AppService } from './app.service'
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('/instagram/profiles/:profileHandle/')
fetchFollowerCount(@Param('profileHandle') profileHandle?: string) {
if (!profileHandle)
throw new BadRequestException('Invalid ProfileHandle provided')
return this.appService.fetchFollowerCount(profileHandle)
}
@Get('instagram/:hashtag/')
fetchPosts(@Param('hashtag') hashtag?: string) {
if (!hashtag)
throw new BadRequestException('Invalid ImageLink provided')
return this.appService.fetchPosts(hashtag)
}
}
I already tried to log if the function is even called, but without any result
CodePudding user response:
Your service isn't being injected into the right field. The constructor of your AppsService should be:
export class AppService {
constructor(
private followerCount: FollowerCount,
private hashtagRepository: HashtagRepository,
) {}
}
Previously, the HashtagRepository service was being injected into the private field getPost
which you don't use. See dependency injection in th NestJS documentation.
CodePudding user response:
Just rename the injected service in AppService
from - private getPost: HashtagRepository
to private hashtagRepository: HashtagRepository
should fix it