sql - nodejs - Running multiple queries using mssql -
i have express
api post request containing sql query using mssql. working , returns result json well. issue have, when want call more once while other query still running..
here code:
app.post('/select', (req, res) => { config.database = req.body.db; var sqlquery = `select ${req.body.select} ${req.body.from}`; if (req.body.where !== '' && req.body.where !== undefined) sqlquery += ` ${req.body.where}`; if (req.body.order !== '' && req.body.order !== undefined) sqlquery += ` order ${req.body.order}`; console.log(`query: ${sqlquery}`); sql.connect(config).then(pool => { return pool.request().query(sqlquery) }).then(result => { sql.close(); sqldone = true; console.dir(result); res.header('content-type', 'application/json'); res.json(result.recordset); }).catch(err => { sql.close(); sqldone = true; console.log('caught error:'); console.log(err); }); sql.on('error', err => { sql.close(); console.log('sql request error:'); console.log(err); }); });
the error when call react
webapp superagent
is:
error: global connection exists. call sql.close() first.
this irritating have call api synchronously , wait first query return, before can call next one..
does know how run multiple queries @ once? have separate connection running query on 1 db another. same server though.
ok have managed past using wait-until library wait until boolean true suggested @splatten
app.post('/select', (req, res) => { waituntil() .interval(500) .times(60) .condition(() => { return (sqldone ? true : false); }) .done((result) => { sqldone = false; sql.connect(config).then(pool => { console.log(); console.log('connected!'); config.database = req.body.db; var sqlquery = `select ${req.body.select} ${req.body.from}`; if (req.body.where !== '' && req.body.where !== undefined) sqlquery += ` ${req.body.where}`; if (req.body.order !== '' && req.body.order !== undefined) sqlquery += ` order ${req.body.order}`; console.log(`query: ${sqlquery}`); return pool.request().query(sqlquery); }).then(result => { sql.close(); sqldone = true; console.log(); console.log('result:'); console.dir(result.recordset); res.header('content-type', 'application/json'); res.json(result.recordset); }).catch(err => { sql.close(); sqldone = true; console.log(); console.error('caught error:'); console.error(err); res.status(500).send({ error: err }); }); sql.on('error', err => { sql.close(); sqldone = true; console.log(); console.error('sql error:'); console.error(err); res.status(500).send({ error: err }); }); }); });
bit of shame can't run multiple queries @ once guess limitation of sql, not library.
wiki
Comments
Post a Comment