Hi, while trying to add data to the db via a transaction I keep getting a 'TypeError: this.callback is not a function'. It happens on '
/node_modules/pg/lib/query.js:142
return this.callback(err)
'
This is the code I use:
//Create connection
const connectionString = 'postgres://localhost:5432/mm';
const pg = require('pg');
const client = new pg.Client(connectionString);
client.connect();
const { Pool, Client } = require('pg')
const pool = new Pool({
connectionString: connectionString,
})
//Called from a function:
(async () => {
// note: we don't try/catch this because if connecting throws an exception
// we don't need to dispose of the client (it will be undefined)
const client = await pool.connect()
.then(() => {
console.log('successful connection');
})
.catch((err) => {
console.error('Error connecting: %s', err);
});
try {
await client.query('BEGIN')
for (var i = 0; i < csvarray.length - 1; i++)
{
var y = i;
const sqlstring = "INSERT INTO data (A, B, C, D, E, F, G, H, I) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);";
await client.query(sqlstring, [csvarray[y][0],csvarray[y][1],csvarray[y][2],csvarray[y][3],csvarray[y][4],csvarray[y][5],csvarray[y][6],csvarray[y][7]],csvarray[y][8])
}
await client.query('COMMIT')
} catch (e) {
await client.query('ROLLBACK')
throw e
} finally {
client.release()
}
})().catch(e => console.log(e.stack))
You don't need .then() / .catch() with async / await. Plus awaiting the .then(...) will give you undefined as you're not returning the client:
// note: we don't try/catch this because if connecting throws an exception
// we don't need to dispose of the client (it will be undefined)
const client = await pool.connect()
.then(() => {
console.log('successful connection');
})
.catch((err) => {
console.error('Error connecting: %s', err);
});
The parameters for the query must be a single array. You're missing the wrapping [ ... ]:
const sqlstring = "INSERT INTO data (A, B, C, D, E, F, G, H, I) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);";
await client.query(sqlstring, [csvarray[y][0],csvarray[y][1],csvarray[y][2],csvarray[y][3],csvarray[y][4],csvarray[y][5],csvarray[y][6],csvarray[y][7]],csvarray[y][8])
Otherwise the client.query(...) will think that the second parameter (list of parameters) is [csvarray[y][0] and the third parameter (callback upon query completion or error) is csvarray[y][1].
Arguments are the problem, yep. Reformatted for clarity:
await client.query(
sqlstring,
[csvarray[y][0],csvarray[y][1],csvarray[y][2],csvarray[y][3],csvarray[y][4],csvarray[y][5],csvarray[y][6],csvarray[y][7]],
csvarray[y][8]
)
csvarray[y][8] needs to be inside the parameter value array, not the third argument to client.query() (which is the callback). csvarray[y].slice(0, 9) might be appropriate?
(pg should be throwing synchronously as soon as you call client.query with the wrong types, though, instead of having a vague error that loses the stack trace. There鈥檚 an issue open for that somewhere.)
Can't believe I overlooked that.. thank you!
Most helpful comment
Arguments are the problem, yep. Reformatted for clarity:
csvarray[y][8]needs to be inside the parameter value array, not the third argument toclient.query()(which is the callback).csvarray[y].slice(0, 9)might be appropriate?(pg should be throwing synchronously as soon as you call
client.querywith the wrong types, though, instead of having a vague error that loses the stack trace. There鈥檚 an issue open for that somewhere.)