I want to store key value pairs to a MongoDB using TypeORM. My backend API is made with NestJs (not sure if this is important). The key is of type string
and the value is of type object
because I want to store anything to this object.
This is my TypeORM database configuration
TypeOrmModule.forRoot({
type: 'mongodb',
host: 'localhost',
port: 27017,
database: 'nest',
entities: [`${__dirname}/**/*.entity.{ts,js}`],
synchronize: true,
}),
My mapping entity does not auto generate the key because I want to set it on my own
@Entity()
export class Mapping extends BaseEntity {
@PrimaryColumn()
key: string;
@Column()
value: object;
}
When I want to save a new mapping to the database I run this logic (it doesn't matter where)
const newMapping: Mapping = new Mapping();
newMapping.key = 'foobar';
newMapping.value = { foo: 'bar' };
await newMapping.save();
I'm getting this error
TypeError: Cannot read property 'propertyName' of undefined
when executing the save
function of the database entity. It seems that I can't store the value of type object
to the database. Any solutions for this?
key
toid
? – hlfrmnid
instead ofkey
now. Unfortunately the error remains the same – hrp8sfH4xQ4PrimaryColumn
toObjectIdColumn
? – hlfrmn