Airton Alonso
Airton Alonso

Reputation: 3

Node.js / React - Cannot read property 'map' of undefined

I'm studying React/Node.js for the first time. I'm getting the following error when trying to access my application:

(node:10188) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'map' of undefined

The error is in the following code:

import Image from '../models/Image';

export default {
  render(image: Image) {
    return {
      id: image.id,
      url: `http://localhost:3333/uploads/${image.path}`,
    };
  },

  renderMany(images: Image[]) {
    return images.map((image) => this.render(image));
  },
};

Apparently, the problem is in the "images.map", but I have no idea how to fix it.

That "Image" component is being imported from this file:

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import Orphanage from './Orphanage';

@Entity('images')
export default class Image {
    @PrimaryGeneratedColumn('increment')
    id: number;

    @Column()
    path: string;

    @ManyToOne(()=> Orphanage, orphanage => orphanage.images)
    @JoinColumn({ name: 'orphanage_id' })
    orphanage: Orphanage;
}

Does anyone know how can I solve this?

Thank you!

Upvotes: 0

Views: 1125

Answers (1)

jfriend00
jfriend00

Reputation: 707876

Go look at where renderMany() is being called and make sure that it's being passed an array. This error indicates that you're not passing anything to it when it expects an array.

The only place in this code where there's a .map() is in renderMany(). So, if that's where the problem is, then we would need to see where you call renderMany() and see what is being passed to it. If that's the source of the error, then it looks like nothing is being passed to renderMany() when it is expecting an array.

Upvotes: 0

Related Questions