TypeError: Cannot read properties of undefined

I'm getting the following error when I try to insert into a view.
"TypeError: Cannot read properties of undefined (reading 'entity_cagematch_id')\n at C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\node_modules\\drizzle-orm\\pg-core\\query-builders\\insert.cjs:62:122\n at Array.map (<anonymous>)\n at PgInsertBuilder.values (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\node_modules\\drizzle-orm\\pg-core\\query-builders\\insert.cjs:57:33)\n at EntityEloManager.insertELOEntities (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\managers\\EntityEloManager.js:149:58)\n at updateEloToDate (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\index.js:37:49)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async start (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\index.js:52:9)"
"TypeError: Cannot read properties of undefined (reading 'entity_cagematch_id')\n at C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\node_modules\\drizzle-orm\\pg-core\\query-builders\\insert.cjs:62:122\n at Array.map (<anonymous>)\n at PgInsertBuilder.values (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\node_modules\\drizzle-orm\\pg-core\\query-builders\\insert.cjs:57:33)\n at EntityEloManager.insertELOEntities (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\managers\\EntityEloManager.js:149:58)\n at updateEloToDate (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\index.js:37:49)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async start (C:\\Users\\manue\\Documents\\wrestle-stonks\\wrestlestonks\\index.js:52:9)"
This is the code that makes the insert:
async insertELOEntities(entities){
try{
//const xata = getXataClient();
const sql = neon(process.env.DATABASE_URL_TEST);
const db = drizzle({ client: sql });
const eloEntities = entities.map(entity => ({
entity_cagematch_id: entity.id,
type_id: getEntityTypeByDescription(entity.type).id,
entity_name: entity.name,
elo_date: entity.lastMatchDate,
elo_prev_elo: entity.oldElo,
elo_new_elo: entity.newELO
}));
const inserted = await db.insert(vEntityEloView).values(eloEntities).execute();
if(inserted.rowCount > 0){
return inserted.rowCount;
}
return inserted;
} catch(e){
logger.error({message: e.message, stack: e.stack});
}
return null;
}
async insertELOEntities(entities){
try{
//const xata = getXataClient();
const sql = neon(process.env.DATABASE_URL_TEST);
const db = drizzle({ client: sql });
const eloEntities = entities.map(entity => ({
entity_cagematch_id: entity.id,
type_id: getEntityTypeByDescription(entity.type).id,
entity_name: entity.name,
elo_date: entity.lastMatchDate,
elo_prev_elo: entity.oldElo,
elo_new_elo: entity.newELO
}));
const inserted = await db.insert(vEntityEloView).values(eloEntities).execute();
if(inserted.rowCount > 0){
return inserted.rowCount;
}
return inserted;
} catch(e){
logger.error({message: e.message, stack: e.stack});
}
return null;
}
3 Replies
SEBAS
SEBASOP2mo ago
And this is the schema for the view:
const vEntityEloView = pgView('v_entity_elo', {
elo_id: integer('elo_id'),
entity_id: integer('entity_id'),
name_id: integer('name_id'),
entity_name: text('entity_name'),
entity_cagematch_id: integer('entity_cagematch_id'),
entity_type: text('entity_type'),
type_id: integer('type_id'),
elo_prev_elo: integer('elo_prev_elo'),
elo_new_elo: integer('elo_new_elo'),
elo_difference: integer('elo_difference'),
elo_date: date('elo_date'),
elo_current: boolean('elo_current'),
elo_created_date: timestamp('elo_created_date'),
elo_edited_date: timestamp('elo_edited_date')
}).existing();
const vEntityEloView = pgView('v_entity_elo', {
elo_id: integer('elo_id'),
entity_id: integer('entity_id'),
name_id: integer('name_id'),
entity_name: text('entity_name'),
entity_cagematch_id: integer('entity_cagematch_id'),
entity_type: text('entity_type'),
type_id: integer('type_id'),
elo_prev_elo: integer('elo_prev_elo'),
elo_new_elo: integer('elo_new_elo'),
elo_difference: integer('elo_difference'),
elo_date: date('elo_date'),
elo_current: boolean('elo_current'),
elo_created_date: timestamp('elo_created_date'),
elo_edited_date: timestamp('elo_edited_date')
}).existing();
The strange part is that none of the parameters are null, because I previously successfully inserted them to the original table Another thing to note, is that the view has an insert trigger, which was also tested with those same parameters
TOSL
TOSL2mo ago
You have to export everything you want drizzle to know about export const vEntityEloView
SEBAS
SEBASOP2mo ago
Sorry, didn't show all the file, yeah I exported the view:
const { pgView, integer, text, date, boolean, timestamp } = require('drizzle-orm/pg-core');

const vEntityEloView = pgView('v_entity_elo', {
elo_id: integer('elo_id'),
entity_id: integer('entity_id'),
name_id: integer('name_id'),
entity_name: text('entity_name'),
entity_cagematch_id: integer('entity_cagematch_id'),
entity_type: text('entity_type'),
type_id: integer('type_id'),
elo_prev_elo: integer('elo_prev_elo'),
elo_new_elo: integer('elo_new_elo'),
elo_difference: integer('elo_difference'),
elo_date: date('elo_date'),
elo_current: boolean('elo_current'),
elo_created_date: timestamp('elo_created_date'),
elo_edited_date: timestamp('elo_edited_date')
}).existing();

module.exports = vEntityEloView;
const { pgView, integer, text, date, boolean, timestamp } = require('drizzle-orm/pg-core');

const vEntityEloView = pgView('v_entity_elo', {
elo_id: integer('elo_id'),
entity_id: integer('entity_id'),
name_id: integer('name_id'),
entity_name: text('entity_name'),
entity_cagematch_id: integer('entity_cagematch_id'),
entity_type: text('entity_type'),
type_id: integer('type_id'),
elo_prev_elo: integer('elo_prev_elo'),
elo_new_elo: integer('elo_new_elo'),
elo_difference: integer('elo_difference'),
elo_date: date('elo_date'),
elo_current: boolean('elo_current'),
elo_created_date: timestamp('elo_created_date'),
elo_edited_date: timestamp('elo_edited_date')
}).existing();

module.exports = vEntityEloView;
It worked for the select, but for some reason it doesn't work when I try to insert it I eventually used the sql template to use the insert and it worked as expected:
async insertELOEntities(entities){
try{
const sqlClient = neon(process.env.DATABASE_URL_TEST);
const db = drizzle({ client: sqlClient });
const eloEntities = entities.map(entity => ({
entity_cagematch_id: entity.id,
type_id: getEntityTypeByDescription(entity.type).id,
entity_name: entity.name,
elo_date: entity.lastMatchDate,
elo_prev_elo: entity.oldElo,
elo_new_elo: entity.newELO
}));
const sqlChunk = [];
sqlChunk.push(sql`INSERT INTO ${vEntityEloView} `);
sqlChunk.push( sql`(entity_cagematch_id,type_id,entity_name,elo_date,elo_prev_elo,elo_new_elo) values `);
for(let i = 0; i < eloEntities.length; i++){
const entity = eloEntities[i];
sqlChunk.push(sql`(${entity.entity_cagematch_id},${entity.type_id},${entity.entity_name},${entity.elo_date},${entity.elo_prev_elo},${entity.elo_new_elo})`);
if(i < eloEntities.length - 1){
sqlChunk.push(sql`,`);
}
}
const response = await db.execute(sql.join(sqlChunk));
if(response.rowCount > 0){
return response.rowCount;
}
return response;
} catch(e){
logger.error({message: e.message, stack: e.stack});
}
return null;
}
async insertELOEntities(entities){
try{
const sqlClient = neon(process.env.DATABASE_URL_TEST);
const db = drizzle({ client: sqlClient });
const eloEntities = entities.map(entity => ({
entity_cagematch_id: entity.id,
type_id: getEntityTypeByDescription(entity.type).id,
entity_name: entity.name,
elo_date: entity.lastMatchDate,
elo_prev_elo: entity.oldElo,
elo_new_elo: entity.newELO
}));
const sqlChunk = [];
sqlChunk.push(sql`INSERT INTO ${vEntityEloView} `);
sqlChunk.push( sql`(entity_cagematch_id,type_id,entity_name,elo_date,elo_prev_elo,elo_new_elo) values `);
for(let i = 0; i < eloEntities.length; i++){
const entity = eloEntities[i];
sqlChunk.push(sql`(${entity.entity_cagematch_id},${entity.type_id},${entity.entity_name},${entity.elo_date},${entity.elo_prev_elo},${entity.elo_new_elo})`);
if(i < eloEntities.length - 1){
sqlChunk.push(sql`,`);
}
}
const response = await db.execute(sql.join(sqlChunk));
if(response.rowCount > 0){
return response.rowCount;
}
return response;
} catch(e){
logger.error({message: e.message, stack: e.stack});
}
return null;
}

Did you find this page helpful?