0

I have the following Data entity:

@PrimaryGeneratedColumn() 
id: number 

@Column() 
dataA: string 

@Column() 
dataB: string 

@Column() 
dataC: number

@Column() 
dataD: number

The data object I am trying to save:

const data = {
      dataA: "data 1",
      nestedData: {
        dataB: "data 2",
        dataC: 3
      },
      dataD: 4
      }

I then try to save it as follows:

await this.dataRepository.save(data)

I get an error that says something about dataB and dataC not being a part of Data entity even though it should be. Thanks.

2 Answers 2

3

You need to flatten it, write a function to do that before passing object into the Repository.

export function flattenData(data) {
    return {
        dataA: data.dataA,
        dataB: data.nestedData.dataB,
        dataC: data.nestedData.dataC,
        dataD: data.dataD,
    }
}

// then pass into Repository
await this.dataRepository.save(flattenData(data));
Sign up to request clarification or add additional context in comments.

Comments

0

There is a 'json' type for the column type: https://typeorm.io/entities#column-types-for-mysql--mariadb

You can then write something like

@Column({ type: 'json' }) ressources: { title: string; ref: string }[];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.