Open more then 1 connection at the same time MSSQL-NODE - node.js

Im using mssql with npm
https://www.npmjs.com/package/mssql
for the start i needed to insert to only one table.
so this is how i used:
const pool_db1 = await sql.connect(config);
pool.request()
.input('input_parameter',element.CustomerID)
.query('INSERT ..')
Now i need to insert some data for one more table that located in different Databases.
So basically i have 2 configs files.
i tried to add
const pool_db1 = await sql.connect(config);
const pool_db2 = await sql.connect(config2);
i know i can SQL.close() one connection and then sql.connect() to another,
but i want another solution because this INSERT to tables happend async for more tables(in the same connection),
so i need those 2 connection to be open but it dosent seems to open 2 connection.
error below:
(node:13888) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): Error: Global connection already exists.
Call sql.close() first. warning.js:18 (node:13888) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.

Apparently when you use sql.conenct you are using the lib in a global scope way, checking docuemntation I found the ConnectionPool method, try:
const pool_db1 = await new sql.ConnectionPool(config).connect();
const pool_db2 = await new sql.ConnectionPool(config2).connect();

Related

Cant read property 'get' of undefined in nodejs

I am using Neo4j for the first time with nodejs and developing an application. I am getting the following error. I tried my best. Can you please help me with this error?
Error
(node:16148) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of
undefined
at E:\fyps\app.js:120:33
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:16148) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error
originated either by throwing inside of an async function without a catch block, or by
rejecting a promise which was not handled with .catch(). To terminate the node process
on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16148) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.js
process with
a non-zero exit code.
code
app.get('/person/:id',function(req,res){
var id = req.params.id;
session
.run("MATCH (a:Person) WHERE id(a)=$idParam RETURN a.name as name", {idParam: id})
.then(function(result){
var name =result.records[0].get("name");
session
.run("OPTIONAL MATCH (a:Person)- [r:BORN_in]-(b:location) where id(a)=$idParam RETURN b.city as city, b.state as state" , {idParam:id})
.then(function(result2){
var city =result2.records[0].get("city");
var state =result2.records[0].get("state");
session
.run("OPTIONAL MATCH (a:Person)-[r:FRIENDS]-(b:Person) WHERE id(a)=$idParam RETURN b", {idParam:id})
.then(function(result3){
var friendsArr =[];
result3.records.forEach(function(record){
if(record._fields[0] != null){
friendsArr.push({
id: record._fields[0].identity.low,
name: record._fields[0].properties.name
});
}
})
res.render('person',{
id:id,
name:name,
city:city,
state:state,
friends:friendsArr
})
session.close();
})
.catch(function(error){
console.log(error);
})
can you please help me that how to solve this error.. Thanks
The error message clearly says that you are calling the get function on some entity that is undefined, and that has happened within a Promise. So, the error statement can be any one of these:
var name =result.records[0].get("name");
var city =result2.records[0].get("city");
var state =result2.records[0].get("state");
Add some relevant log statements to check which one is causing the issue.

Error creating database pool not creating session

Development environment: nodejs
Library: oracle-db
Oracle Version : 9i
The test and error occurrence codes are as follows:
const oracledb = require("oracledb");
async function hello() {
await oracledb.createPool({
user: "test",
password: "1234", // myhrpw contains the hr schema password
connectString: "127.0.0.1/ORCL",
poolAlias: "ora1",
poolIncrement: 0,
poolMax: 4,
poolMin: 4,
poolPingInterval: 30,
});
const connection = await oracledb.getConnection("ora1");
const result = await connection.execute(`SELECT IDX FROM MYTABLE WHERE number= '000000000'`);
console.log(result.rows);
connection.release();
}
hello();
The database pool was created with this code. And after the test, four sessions were created and confirmed to work well. However, an error occurred due to repeated execution of the code. If only three or fewer sessions are created when creating the pool, the following error occurs:
(node:27732) UnhandledPromiseRejectionWarning: Error: ORA-12170: TNS:Connect timeout occurred
(node:27732) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:27732) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
In order to check what kind of error has occurred, I have tailed the file below.
#tail -f /oracle/app/oracle/admin/orcl/bdump/alert_orcl.log
#tail -f /oracle/app/oracle/product/9i/rdbms/log/alert_orcl.log
However, no logs were stacked and no errors were seen. Could you possibly help me?

Discord.js v12 server member count

(Welcome command using canvas)
How could I fetch the server member count as soon as someone joins??
Because I use that line of code
const guild = client.guilds.cache.get("843190900930510869");
let image = await welcomeCanvas
.setUsername(member.user.tag)
.setDiscriminator(member.user.discriminator)
.setMemberCount(guild.memberCount) //this line
etc...
And well, it just doesn't send the image..
Error:
(node:6387) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'memberCount' of undefined
at GuildMemberAddListener.exec (/app/listeners/guildMemberAdd.js:100:29)
(node:6387) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6387) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
The event client.on('guildMemberAdd', () => {}) will return a GuildMember object. Simply use this GuildMember object to get the guild they entered GuildMember.guild then check if that guild is available to the client using guild.available. If it is available you can access all the properties on that guild including the guild.memberCount property.
client.on('guildMemberAdd', (member) => {
const guild = member.guild
if (!guild.available) return console.error('Uh Oh Stinky...')
const guildMemberCount = guild.memberCount
console.log(guildMemberCount)
})

Node.JS RequestError: No connection is specified for that request random error

I have the following code to perform a query on SQL server:
await sql.connect(process.env.DB_CSTRING)
let recordset = await sql.query(`select * FROM dbo.users WHERE (userId = ${userData.userId})
`)
if (recordset.rowsAffected[0] > 0) {
JsonResponse.success = 1
JsonResponse.agentData = recordset.recordset[0]
}
await sql.close();
So whille I was trying to call this API several times (by clicking on an angular app button constantly), I suddenly got the following error:
Server running at http://0.0.0.0:3000/ (node:8180)
UnhandledPromiseRejectionWarning: RequestError: No connection is
specified for that request.
I fixed it with a try-catch as per the error message below, but even if the problem is fixed, I would like to learn why this is happening and if I am doing something wrong with the way Im performing the SQL connection and query:
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled
promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 9)
Thanks.
Awaiting or .thening the pool creation is a safe way to ensure that the pool is always ready, without knowing where it is needed first. In practice, once the pool is created then there will be no delay for the next operation.
new sql.ConnectionPool(process.env.DB_CSTRING).connect().then(pool => {
return pool.query`select * FROM dbo.users WHERE (userId = ${userData.userId} `
}).then(result => {
console.dir(result)
}).catch(err => {
// ... error checks
})
All values are automatically sanitized against sql injection.
Source:
https://github.com/tediousjs/node-mssql#connection-pools

Explicit wait for a selector isn't working?

I'm writing a code to log in Gmail. On the password page, instead of using implicit wait, I want to use explicit wait instead. However, it is not picking up my selector?
(async () => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto('https://accounts.google.com/');
await page.$('#identifierId');
await page.keyboard.type('Test1234');
await page.click('#identifierNext > content > span');
await page.waitForSelector('#password'); //this doesnt work
// await page.waitFor(5000); this works
await page.$('#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input');
await page.keyboard.type('fakePassword');
await page.click('#passwordNext > content');
);
I'm getting the error:
(node:14428) UnhandledPromiseRejectionWarning: Error: Node is either not visible or not an HTMLElement
at ElementHandle._clickablePoint (/Users/asd/Projects/FreeRazor/node_modules/puppeteer/lib/JSHandle.js:199:13)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
-- ASYNC --
at ElementHandle. (/Users/asd/Projects/FreeRazor/node_modules/puppeteer/lib/helper.js:110:27)
at DOMWorld.click (/Users/asd/Projects/FreeRazor/node_modules/puppeteer/lib/DOMWorld.js:367:18)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
-- ASYNC --
at Frame. (/Users/asd/Projects/FreeRazor/node_modules/puppeteer/lib/helper.js:110:27)
at Page.click (/Users/asd/Projects/FreeRazor/node_modules/puppeteer/lib/Page.js:988:29)
at /Users/asd/Projects/FreeRazor/app.js:19:16
at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:14428) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14428) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
The page.waitForSelector statement is working. One of the page.click calls is the problem.
Relevant part of the error message:
(node:14428) UnhandledPromiseRejectionWarning: Error: Node is either not visible or not an HTMLElement
[...]
at Page.click (/Users/asd/Projects/FreeRazor/node_modules/puppeteer/lib/Page.js:988:29)
at /Users/asd/Projects/FreeRazor/app.js:19:16
So the error happens in line 19. I don't know for sure which line it is, but I'm assuming it is the latter page.click call as you are saying the code works if you wait longer (page.waitFor(5000)). So it seems it takes the page longer to display the #passwordNext > content DOM element than the #password element.
Solution
You can solve this problem by putting another waitForSelector before your click to make sure the element actually exists. I even added the option { visible: true } to make sure the DOM node is also visible:
await page.waitForSelector('#passwordNext > content', { visible: true });
await page.click('#passwordNext > content');

Resources