I am trying to insert multiple rows in PostgreSQL using node pg. I am using transactions but my query is executing after a response. I tried async await with my function but it is not working
This is my function
addPersons = async (req, res) => {
try {
await db.query("BEGIN");
req.body.forEach((person, index) => {
if (person.id) {
try {
await db.query("ROLLBACK");
} catch (error) {
console.error("Error rolling back client", err.stack);
}
return res
.status(Error_code.IdNotFound.code)
.send(Error_code.IdNotFound);
}
const query = `update person set
name = ${person.name},
where id = '${
person.id
}'`;
try {
await db.query(query);
} catch (error) {
try {
await db.query("ROLLBACK");
} catch (error) {
console.error("Error rolling back client", err.stack);
}
return res.status(500).send(err);
}
})
await db.query("COMMIT");
res.status(Error_code.Successfull.code).send(Error_code.Successfull);
} catch (error) {
try {
db.query("ROLLBACK");
} catch (error) {
console.error("Error rolling back client", err.stack);
}
return res
.status(Error_code.UnableToBeginTransaction.code)
.send(Error_code.UnableToBeginTransaction);
}
}
I also tried calling this function from another function and using foreach on that function but when whenever code detects await or callback in the second function it does not wait and return to the first function.
How can I run this code to add my data into PostgreSQL with transactions
Thanks
Since this is tagged node-postgres, I suggest that you base your code on the A pooled client with async/await example in the node-postgres documentation. I also suggest that you use parameterized queries or a query builder such as mongo-sql. (There are many, but that one's my favourite. 🙂)
It could look something like this:
const { Pool } = require("pg");
const pool = new Pool();
const addPersons = async (req, res) => {
const db = await pool.connect();
try {
await db.query("BEGIN");
const query = `update person set name = $1 where id = $2;`;
// Promise.all() may improve performance here, but I'm not sure if it's safe
// or even useful in the case of transactions.
for (const person of req.body) {
await db.query(query, [person.name, person.id]);
}
await db.query("COMMIT");
} catch (e) {
await db.query("ROLLBACK");
throw e;
} finally {
db.release();
}
};
Related
I don't know much Javascript and was making a nodejs app. My mongodb query in nodejs is working only when the query has a function method like .toArray
Here's the database.js file
const {MongoClient} = require('mongodb');
const uri = "mongodb+srv://name:pass#clusterurl/metro4?retryWrites=true&w=majority";
// all fields are correctly filled
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
client.connect(err =>{
if(err) throw err;
let db = client.db('metro4');
db.collection('Station').find().toArray(function(err, result){
if(err) throw err;
console.log(result);
});
let a = db.collection('Station').findOne({'_id':4});
if(a) {
console.log(a);
}
else{
console.log("No a\n");
}
module.exports = db;
});
} catch (e) {
console.error(e);
} finally {
client.close();
}
when I run the app, the db.collection('Station').find().toArray runs fine and output the result but the second query of findOne doesn't work.
Any help is appreciated.
The findOne method returns a Promise. You should handle its result in a callback function:
db.collection('Station').findOne({ _id: 4 }, function (err, a) {
if (err) {
console.log(err);
} else if (a) {
console.log(a);
} else {
console.log('No a\n');
}
});
Or using async - await:
client.connect(async (err) => {
...
let a = await db.collection('Station').findOne({ _id: 4 })
...
});
EDIT
To handle the import - export problem you should handle the datase connection operations separate async functions.
You may use the connection function to return the database instance:
const {MongoClient} = require('mongodb');
const uri = "mongodb+srv://name:pass#clusterurl/metro4?retryWrites=true&w=majority";
// all fields are correctly filled
const client = new MongoClient(uri);
const connectDB = async () => {
try {
// Connect to the MongoDB cluster
await client.connect();
return client.db('metro4');
} catch (e) {
throw e;
}
}
const disconnectDB = () => {
client.close();
}
module.exports = { connectDB, disconnectDB };
Then use these functions to handle your database related operations:
const { connectDB, disconnectDB } = require('../database');
const getStations = async () => {
const db = connectDB();
if (!db) return;
try {
const data = await db.collection('Station').find().toArray();
return data;
} catch (err) {
throw err;
} finally {
disconnectDB();
}
}
const getStation = async (id) => {
const db = connectDB();
if (!db) return;
try {
const data = await db.collection('Station').findOne({ _id: id});
return data;
} catch (err) {
throw err;
} finally {
disconnectDB();
}
}
If my controller makes multiple db queries in the same async function should each db query be wrapped in it's own individual try/catch block or is it fine to have all db queries in the same try/catch? What is the reasoning for either option?
All db queries in their own try/catch example:
const confirmEmailVerification = async (req, res) => {
const { token } = req.body;
let user;
try {
const result = await db.query(
'SELECT user_account_id FROM user_account WHERE email_verification_token = $1',
[token]
);
if (result.rows.length === 0) {
return res
.status(400)
.json('Please verify your account by clicking the link in your email');
}
user = result.rows[0].user_account_id;
} catch (err) {
console.error(err.message);
return res.status(500).json('Server Error');
}
try {
const active = await db.query(
'UPDATE user_account SET email_verified = TRUE WHERE user_account_id = $1',
[user]
);
return res.status(200).json({
message: 'Email has been verified, Please login',
});
} catch (err) {
console.error(err.message);
return res.status(500).json('Server Error');
}
};
All db queries in the same try/catch example:
const confirmEmailVerification = async (req, res) => {
const { token } = req.body;
let user;
try {
const result = await db.query(
'SELECT user_account_id FROM user_account WHERE email_verification_token = $1',
[token]
);
if (result.rows.length === 0) {
return res
.status(400)
.json('Please verify your account by clicking the link in your email');
}
user = result.rows[0].user_account_id;
const active = await db.query(
'UPDATE user_account SET email_verified = TRUE WHERE user_account_id = $1',
[user]
);
return res.status(200).json({
message: 'Email has been verified, Please login',
});
} catch (err) {
console.error(err.message);
return res.status(500).json('Server Error');
}
};
This depends on that, if you want the sequence of functions to continue after previous function throws error.
In your case it is useless, because in either errors you finish with res.status(500).json('Server Error').
But sometimes you want to continue, even if some of the functions in the chain throws error, eg.:
let errors = []
try {
f1()
} catch (e) {
errors.push(e)
}
try {
f2()
} catch (e) {
errors.push(e)
}
try {
f3()
} catch (e) {
errors.push(e)
}
If you put this is in one try/catch block, you would stop on the error of f1() and f2() and f3() would not be ran at all.
try {
f1()
f2()
f3()
} catch (e) {
something...
}
I am processing multiple records using async/await and for parallel using Promise.all see my sample code below
let results = [100,200]
let promises = results.map(async record => {
try {
return await processingRecords(record);
} catch (err) {
}
});
await Promise.all(promises);
async function processingRecords(item) {
switch (item['#type']) {
case 'case1':
await Request1(item)
await Request2(item)
break
case 'case2':
await Request3(item)
}
}
But the problem is if Request1 is getting any error I can't catch error from Request2 call how to handle error from both calls
You can to do a few things here to keep the calls going. Catching errors around the await statements and returning the combined result of Request1 and Request2 will work:
For example:
async function processingRecords(item) {
switch (item['#type']) {
case 'case1':
let combinedResult = {};
try {
combinedResult.Request1Result = await Request1(item);
} catch (err) {
combinedResult.Request1Error = err;
}
try {
combinedResult.Request2Result = await Request2(item);
} catch (err) {
combinedResult.Request2Error = err;
}
// Keep the promise chain intact.;
return combinedResult;
case 'case2':
return await Request3(item);
}
}
let promises = results.map(async record => {
try {
return await processingRecords(record);
} catch (err) {
// Keep the promise chain intact by throwing err here.
throw err;
}
});
let overallResult = await Promise.all(promises);
console.log(overallResult);
I am new in Node.js, I have async method called
async function makeLineStringFromPoints(user) {
...
for (let item of linesGeoJson) {
saveLineIntoDb({
'line': item,
'date': user.date_created,
'user_id': user.uid,
'device_id': user.devid,
}).then((userStored) => {
console.log(userStored); // i received undefined
}).catch((err) => {
console.log(err);
});
}
...
}
in this method I have invoke other async method saveLineIntoDb in this method I an storing user information in database :
async function saveLineIntoDb(user) {
let userStored = user;
try {
await db.result(pgp.helpers.insert(user, cs))
.then(data => {
return await (user); // return user
});
} catch (error) {
logger.error('saveIntoDatabase Error:', error);
}
}
Now after storing I want to return user in saveLineIntoDb(user) to }).then((userStored) but always I got undefined.
How can I do that ?
saveLineIntoDb doesn't return the result and mixes async and raw promises. It also prevents errors from being handled in caller function. return await (user) is excessive, it also won't work because it happens inside regular function.
It should be:
async function saveLineIntoDb(user) {
await db.result(pgp.helpers.insert(user, cs));
return user;
}
While caller function doesn't chain promises and doesn't make use of await. If DB queries should be performed in series, it should be:
async function makeLineStringFromPoints(user) {
...
try {
for (let item of linesGeoJson) {
const userStored = await saveLineIntoDb(...);
console.log(userStored);
}
} catch (err) {
console.log(err);
}
}
If DB queries should be performed in parallel, it should be:
async function makeLineStringFromPoints(user) {
...
try {
await Promise.all(linesGeoJson.map(async (item) => {
const userStored = await saveLineIntoDb(...));
console.log(userStored);
}));
} catch (err) {
console.log(err);
}
}
Why you are returning user inside the then? May be can do in this way
async function saveLineIntoDb(user) {
let userStored = user;
try {
await db.result(pgp.helpers.insert(user, cs));
return user; // return user
} catch (error) {
logger.error('saveIntoDatabase Error:', error);
}
}
In saveLineIntoDb method try return result of await:
async function saveLineIntoDb(user) {
let userStored = user;
try {
return await db.result(pgp.helpers.insert(user, cs))
.then(data => {
return await (user); // return user
});
} catch (error) {
logger.error('saveIntoDatabase Error:', error);
}
}
(This function, you don't need use async/await)
To overcome callback hell in javascript, I'm trying to use async await from legacy code written in SQLServer procedure.
But I'm not sure my code might be write properly.
My first confusing point is when async function returns, should it return resolve() as boolean, or just return reject and handle with try-catch?
Here is my code snippets.
Please correct me to right direction.
apiRoutes.js
app.route('/api/dansok/cancelDansok')
.post(dansokCancelHandler.cancelDansok);
dansokCancelController.js
const sequelize = models.Sequelize;
const jwt = require('jsonwebtoken');
async function jwtAccessAuthCheck(accessToken) {
if (!accessToken) {
return Promise.reject('Empty access token');
}
jwt.verify(accessToken,"dipa",function(err){
if(err) {
return Promise.reject('TokenExpiredError.');
} else {
return Promise.resolve();
}
});
}
async function checkFeeHist(dansokSeqNo) {
let feeHist = await models.FeeHist.findOne({
where: { DansokSeqNo: dansokSeqNo}
});
return !!feeHist;
}
async function getNextDansokHistSerialNo(dansokSeqNo) {
....
}
async function getDansokFee(dansokSeqNo) {
....
}
async function doCancel(dansokSeqNo) {
try {
if (await !checkFeeHist(dansokSeqNo)) {
log.error("doCancel() invalid dansokSeqNo for cancel, ", dansokSeqNo);
return;
}
let nextDansokSerialNo = await getNextDansokHistSerialNo(dansokSeqNo);
await insertNewDansokHist(dansokSeqNo, nextDansokSerialNo);
await updateDansokHist(dansokSeqNo);
await updateVBankList(dansokSeqNo, danokFee.VBankSeqNo);
await getVBankList(dansokSeqNo);
} catch (e) {
log.error("doCancel() exception:", e);
}
}
exports.cancelDansok = function (req, res) {
res.setHeader("Content-Type", "application/json; charset=utf-8");
const dansokSeqNo = req.body.DANSOKSEQNO;
const discKindCode = req.body.HISTKIND;
const worker = req.body.PROCWORKER;
const workerIp = req.body.CREATEIP;
const accessToken = req.headers.accesstoken;
//check input parameter
if (!dansokSeqNo || !discKindCode || !worker || !workerIp) {
let e = {status:400, message:'params are empty.'};
return res.status(e.status).json(e);
}
try {
jwtAccessAuthCheck(accessToken)
.then(() => {
log.info("jwt success");
doCancel(dansokSeqNo).then(() => {
log.info("cancelDansok() finish");
res.status(200).json({ message: 'cancelDansok success.' });
});
});
} catch(e) {
return res.status(e.status).json(e);
}
};
You'll need to rewrite jwtAccessAuthCheck(accessToken) so that it keeps track of the outcome of its nested tasks. In the code you've written:
// Code that needs fixes!
async function jwtAccessAuthCheck(accessToken) {
// This part is fine. We are in the main async flow.
if (!accessToken) {
return Promise.reject('Empty access token');
}
// This needs to be rewritten, as the async function itself doesn't know anything about
// the outcome of `jwt.verify`...
jwt.verify(accessToken,"dipa",function(err){
if(err) {
// This is wrapped in a `function(err)` callback, so the return value is irrelevant
// to the async function itself
return Promise.reject('TokenExpiredError.');
} else {
// Same problem here.
return Promise.resolve();
}
});
// Since the main async scope didn't handle anything related to `jwt.verify`, the content
// below will print even before `jwt.verify()` completes! And the async call will be
// considered complete right away.
console.log('Completed before jwt.verify() outcome');
}
A better rewrite would be:
// Fixed code. The outcome of `jwt.verify` is explicitly delegated back to a new Promise's
// `resolve` and `reject` handlers, Promise which we await for.
async function jwtAccessAuthCheck(accessToken) {
await new Promise((resolve, reject) => {
if (!accessToken) {
reject('Empty access token');
return;
}
jwt.verify(accessToken,"dipa",function(err){
if(err) {
reject('TokenExpiredError.');
} else {
resolve();
}
});
});
// We won't consider this async call done until the Promise above completes.
console.log('Completed');
}
An alternate signature that would also work in this specific use case:
// Also works this way without the `async` type:
function jwtAccessAuthCheck(accessToken) {
return new Promise((resolve, reject) => {
...
});
}
Regarding your cancelDansok(req, res) middleware, since jwtAccessAuthCheck is guaranteed to return a Promise (you made it an async function), you'll also need to handle its returned Promise directly. No try / catch can handle the outcome of this asynchronous task.
exports.cancelDansok = function (req, res) {
...
jwtAccessAuthCheck(accessToken)
.then(() => {
log.info("jwt success");
return doCancel(dansokSeqNo);
})
.then(() => {
log.info("cancelDansok() finish");
res.status(200).json({ message: 'cancelDansok success.' });
})
.catch(e => {
res.status(e.status).json(e);
});
};
I strongly suggest reading a few Promise-related articles to get the hang of it. They're very handy and powerful, but also bring a little pain when mixed with other JS patterns (async callbacks, try / catch...).
https://www.promisejs.org/
Node.js util.promisify