Home > Blockchain >  typeorm saving list as string
typeorm saving list as string

Time:05-11

I have a flutter frontend which sends url strings in a list like ['url','url','url'] but in Postgres its saved/on retrieval shows up as a single string like 'url,url,url'.

Here's the column with problem:

  @Column({
    type: "simple-array",
    default: [],
  })
  postMedia!: string[];

Here's the repository method that saves the post:

  async addPost(req: Request, res: Response) {
    let { email } = req.params;
    let postText = req.body.postText;
    let postMedia: string[] = req.body.postMedia;

    let accountRepo = getCustomRepository(AccountRepository);
    let account = await accountRepo.findOne({
      email: email,
    });

    try {
      let post = new PostEntity();
      post.postText = postText;
      post.postMedia = postMedia;
      post.account = account!;
      await post.save();

      return res.send({
        message: "Post Added",
        added: true,
      });
    } catch (error) {
      console.log(error);
      return res.send({
        message: "Something went wrong",
        added: false,
      });
    }
  }

Where is the problem? Why isn't the list saved as array even after I typed the postMedia variable as string[]? please help

CodePudding user response:

This worked for me

@Column("text", { array: true })
postMedia!: string[];

Do let me know if this solves your problem

  • Related