I was run my webscraping code but got this error ??(NodeJS) - node.js

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:5876) [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.
How to solve this type of error any suggestions

Your code throws an error at some point in some promise. You can solve it by properly handling the exception.
Ex:
var p = new Promise((resolve, reject) => {
// Some code
});
p.then(res => {
// No errors thrown
}).catch(err => {
// Exception happened. Print the error
console.log(err);
});

Related

not getting the ops array after inserting into mongo db

when am trying to move the image after inserting into mongo db using the function
var db = require('../config/dbconnect')
module.exports = {
//addProduct:(product,callback)=>
addProduct: (product, callback) => {
//console.log(product)
db.get().collection('product').insertOne(product).then((data) => {
console.log(data)
callback(data.ops[0]._id)
})
}
}
I am getting an error like:
{
acknowledged: true,
insertedId: new ObjectId("61054e43cffb994774d37ab0")
}
(node:6964) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
at E:\Nodejs\ecommerce\helpdesk\product_help.js:11:27
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6964) 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:6964) [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.
This is error first callback. Try the second argument which is the result.
db.get().collection('product').insertOne(product).then((error,data)=>
{
console.log(data)
callback(data.ops[0]._id)
})
instead of callback(data.ops[0]._id).
Try:
db.get().collection('product').insertOne(product).then((data) => {
callback(data.insertedId)
})
.catch((err)=>console.log('venk error',err))
}

Node.js serialport access denied

I am trying to send a string through serialport in node.js with the following code:
function createSerialPort() {
myPort = new SerialPort('COM3', {
baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: 0
})
myPort.write("135");
}
Everytime I run it it gives me this error:
(node:8468) UnhandledPromiseRejectionWarning: Error: Opening COM3: Access denied
(node:8468) 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:8468) [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
I have looked everywhere for a fix to this (I am new to using serialport) and the only solution I have found is to do this:
function createSerialPort() {
myPort = new SerialPort('COM3', {
baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: 0
}, true)
myPort.write("135");
}
That solution then gives me this:
(node:18192) UnhandledPromiseRejectionWarning: TypeError: callback.call is not a function
at SerialPort._error (C:\examplepath\node_modules\#serialport\stream\lib\index.js:198:14)
at C:\examplepath\node_modules\#serialport\stream\lib\index.js:242:12
(node:18192) 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:18192) [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.

Discord.js role reaction

When this is run it gives an error message.
The bot has full admin over the server I am testing it in.
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial); await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if ((reaction.message.channel.id === "744212634446224976") && (reaction.emoji.name === '🚩')) {
reaction.message.guild.members.cache.get(user.id).roles.add('746263081695067178').catch()
}
});
Error message:
(node:17644) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
at RequestHandler.execute (D:\Projects\Bot-role\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:17644) 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:17644) [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 role being changed has to be under the clients (bots) own.
Change the order under [server] > Server Settings > Roles

Adding a finally handler after promise creation results in uncaught promise rejection?

If I have a script like this
function timeout(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
(async () => {
try {
async function throwAfterOneSecond() {
await timeout(1000);
throw new Error("wow");
}
const p1 = throwAfterOneSecond()
p1.finally(() => {
console.log("finally handler");
});
await p1;
} catch (e) {
console.error(e);
}
})();
Then run it, this results in an unhandled promise exception
$ node test.js
finally handler
Error: wow
at throwAfterOneSecond (/home/cdiesh/test.js:13:13)
(node:3657795) UnhandledPromiseRejectionWarning: Error: wow
at throwAfterOneSecond (/home/cdiesh/test.js:13:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3657795) 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: 2)
(node:3657795) [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.
However if I use
const p1 = throwAfterOneSecond().finally(() => {
console.log("finally handler");
});
There is no unhandled promise exception, everything is fine
Does this behavior have any name? Running node v14.7.0

Trying to update a table - Error: Unhandled promise rejection node.js

I'm trying to update a user with a hashed password when I start the app.
So I wrote this in app.js:
try {
bcrypt.hash("ADMIN", saltRounds, async function(err, hash) {
queryUpdate = await Utilisateur.query().patch({
MOTPASS: hash
}).where('NOGENE', 4219)
.catch(console.log('err'));
});
} catch (err) {
errorDbHandler.sendErrorHttp(err, res);
}
And I got this error:
(node:6800) UnhandledPromiseRejectionWarning: TypeError: Utilisateur.query is not a function
at D:\Project\***\backend\app.js:48:37
(node:6800) 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:6800) [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.
use this code for catching the error:
try {
bcrypt.hash("ADMIN", saltRounds, async function(err, hash) {
try {
queryUpdate = await Utilisateur.query().patch({
MOTPASS: hash
}).where('NOGENE', 4219);
} catch (e) {
console.log(e);
}
});
} catch (err) {
errorDbHandler.sendErrorHttp(err, res);
}
you tried to mix "then().catch()" with "await", which will not work.

Resources