Discord.js v12 server member count - node.js

(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)
})

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?

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');

ECONNREFUSED error when loading a TensorFlow frozen model from node.js

I was trying to load a TensorFlow fronzen model from a url that points to not existing resource to test my code robustness. However, even though I have set a catch, I am not able to manage a ECONNREFUSED that is raised internally by the function tf.loadFrozenModel.
Is there any possible mitigation to this issue? This is for me a critical problem, since it stops the execution of nodejs.
Here is the code where the error is generated.
global.fetch = require("node-fetch");
const tf = require("#tensorflow/tfjs");
require("#tensorflow/tfjs-node");
class TFModel {
...
loadFzModel(modelUrl, modelWeigths) {
return tf.loadFrozenModel(modelUrl, modelWeigths)
.then((mod) => {
this.arch = mod;
})
.catch((err) => {
console.log("Error downloading the model!");
});
}
...
}
Here instead are the errors I am getting:
UnhandledPromiseRejectionWarning: Error: http://localhost:30000/webModel/tensorflowjs_model.pb not found. FetchError: request to http://localhost:30000/webModel/tensorflowjs_model.pb failed, reason: connect ECONNREFUSED 127.0.0.1:30000
at BrowserHTTPRequest.<anonymous> (.../node_modules/#tensorflow/tfjs-core/dist/io/browser_http.js:128:31)
at step (.../node_modules/#tensorflow/tfjs-core/dist/io/browser_http.js:32:23)
at Object.throw (.../node_modules/#tensorflow/tfjs-core/dist/io/browser_http.js:13:53)
at rejected (.../node_modules/#tensorflow/tfjs-core/dist/io/browser_http.js:5:65)
at process.internalTickCallback (internal/process/next_tick.js:77:7)
(node:23291) 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:23291) [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.
Note: the code works if modelUrl and modelWeights are valid url pointing to existing resources.
Node-2: the code is executed as part of a custom block for Node-Red.
If you don't find any other solution you can catch the error on the top level like this:
process.on('uncaughtException', function (err) {
console.error(err);
});
In there you can get more specific to only catch your specific error.
This is in the process of being addressed at https://github.com/tensorflow/tfjs-core/pull/1455.

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

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();

Resources