Trying to update a table - Error: Unhandled promise rejection node.js - 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.

Related

I was run my webscraping code but got this error ??(NodeJS)

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

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

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

UnhandledPromiseRejectionWarning: TypeError: res.send is not a function

I am trying to get this aligoapi npm package to work.
when i send request it's showing error
here is my code:
const aligoapi = require('aligoapi');
var AuthData = {
key: '<my_key>',
user_id: '<my_id>',
}
var send = (req, res) => {
console.log('check')
aligoapi.send(req, AuthData)
.then((r) => {
console.log('check1')
res.send(r)
})
.catch((e) => {
console.log('check2')
res.send(e)
})
}
console.log('check3')
var number = {
body: {
sender: '01022558877',
receiver: '01081079508',
msg: 'test msg'
}
}
const result = send(number,'err')
console.log(result)
and this is the terminal output:
justin#Justinui-MacBookPro examples % node test.js
check3
check
undefined
check2
(node:94270) UnhandledPromiseRejectionWarning: TypeError: res.send is not a function
at /Users/justin/Downloads/node.js_exampl/examples/test.js:19:11
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:94270) 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: 3)
(node:94270) [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.
justin#Justinui-MacBookPro examples %
here i notice my request isn't actually being sent because it's returning undefined

Retrieving JSON data from an Axios response

I'm using TypeScript to create a Node.js application and I want to retrieve JSON data from an external API. I have a demo version of the code I'm using, can't put my actual codebase up.
private async getData() {
return await Axios.get(
`http://dummy.restapiexample.com/api/v1/employees`
).then(response => {
return response.data;
});
}
getReleaseResults() {
this.getData().then(responseData => {
responseData.data.data.forEach((element: any) => {
console.log(element);
});
});
}
The error message I get is: (node:6068) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at C:\Users\Caoilinn.Hughes\OneDrive\Documents\TypeScript Demos\Azure Test Result Email Extension\emailAzureExtension\app\out\js\apiCaller.js:43:36
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:6068) 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:6068) [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.
If I don't have the for each and replace it with
console.log(responseData.data.data)
I don't get any issues. FYI the result set has a data property hence the "data.data"
The problem is that you are already returning response.data from your get function and then again you are doing data.data which would not work.
see this. Remove extra data.
getReleaseResults() {
this.getData().then(responseData => {
responseData.data.forEach((element: any) => {
console.log(element);
});
});
}
Add catch block to get the error if there is any. What is the expected response ? try to log it.
return await Axios.get(
`http://dummy.restapiexample.com/api/v1/employees`
).then(response => {
console.log(response);
return response.data;
}).catch(err => console.log(err));

Resources