Why is the typeorm find method returning me a string instead of an array?

Asked

Viewed 74 times

0

I have a tool modem as it lies below:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';

import OrdersTools from './OrdersTools';

@Entity('tools')
class Tool {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  title: string;

  @Column()
  link: string;

  @Column()
  description: string;

  @Column('text', { array: true })
  tags: string[];

  @OneToMany(() => OrdersTools, order_tools => order_tools.tool)
  order_tools: OrdersTools[];

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}

export default Tool;

When I create a new tool and saved in the database I get this reply:

{
  "title": "typescript 3",
  "link": "https://github.com/typicode/json-server",
  "description": "Fake REST API based on a json schema. Useful for mocking and creating APIs for front-end devs to consume in coding challenges.",
  "tags": [
    "api",
    "json",
    "schema",
    "node",
    "github",
    "rest"
  ],
  "id": "24679c86-ce4f-4b21-83d5-d7466dc3ac58",
  "created_at": "2021-03-28T21:57:54.048Z",
  "updated_at": "2021-03-28T21:57:54.048Z"
}

However when I list all tools using the find method the tags attribute of the Tool model is returned as a string and not as an array of strings:

[
  {
    "id": "0df09c18-02ed-43f6-a0d0-32e2d45e47de",
    "title": "hotel",
    "link": "https://github.com/typicode/hotel",
    "description": "Local app manager. Start apps within your browser, developer tool with local .localhost domain and https out of the box.",
    "tags": "{\"node\",\"organizing\",\"webapps\",\"domain\",\"developer\",\"https\",\"proxy\"}",
    "created_at": "2021-03-26T02:58:15.506Z",
    "updated_at": "2021-03-26T02:58:15.506Z"
  }
]

How can I fix this?

  • Came to test with @Column("simple-array") for the column tags: string[] ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.