학이시습

[Nest] Error: Cannot read properties of undefined (reading '') 본문

Framework/Nest

[Nest] Error: Cannot read properties of undefined (reading '')

dbswndud 2025. 5. 23. 09:23


에러!

google vision ai api를 쓰던 도중 아래와 같은 에러를 마주했다.

 

분명 description에는 description: 'Sydney Opera House' 값이 있었는데 값이 정의되어있지 않아서 읽을 수 없다는 것이다.

 

문제점

const [result] = await this.client.landmarkDetection({
	image: { content: image.replace(/^data:image\/\w+;base64,/, '') },
});

const detectedLandmark = result.landmarkAnnotations || []; 
console.log('detectedLandmark : ', detectedLandmark);
if (!detectedLandmark.length || !detectedLandmark[0].description)
	return null;

const preprocessedName = this.preprocessingName(result[0].description); // <-- 이부분
const landmarks = await this.landmarkService.find();

for (const landmark of landmarks) {
const LandmarksName = this.preprocessingName(landmark.name);
    if (
      LandmarksName.includes(preprocessedName) ||
      preprocessedName.includes(LandmarksName)
    ) {
      return landmark.id;
    }
}

return null;

 

description은 result안의 landmarkAnnotations안에 있는데, result[0]에서 찾으려 했기 때문

 

해결

result[0]가 아닌 detectedLandmark[0]로 바꿔 해결했다!