not getting the ops array after inserting into mongo db - node.js

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

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

Node.js Problem with puppeteer headless:True : " Evaluation failed: TypeError: Cannot read property 'querySelectorAll' of null"

My code works perfectly well with headless false, but when I change it to true I get this error :
(node:92024) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'querySelectorAll' of null
at __puppeteer_evaluation_script__:7:17
at ExecutionContext._evaluateInternal (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async ExecutionContext.evaluate (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
at async getAllInfos (C:\wamp64\www\scrappingWithNodejs\testDiscordBot\index.js:28:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:92024) 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:92024) [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.
My code :
async function getAllInfos() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
await page.goto(`https://www.japanread.cc/`, {
waitUntil: ["load", "domcontentloaded", "networkidle0", "networkidle2"],
}); //se rendre sur une page
let trs = [];
trs = tbody.querySelectorAll("tr");//the error is here
I tried to put showMo, put several things in the "waitUntil" but I get nothing, I really don't understand.
Do you know what the problem is?

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

NestJS UnhandledPromiseRejectionWarning by Promise

when running my Get method in both Swagger and Postman, I get the following error message:
(node:35014) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'error' of null
at HttpErrorFilter.catch (/Users/mapoferl/Documents/develop/BuergermeisterApp/backendnest/src/shared/http-error.filter.ts:28:31)
at ExceptionsHandler.invokeCustomFilters (/Users/mapoferl/Documents/develop/BuergermeisterApp/backendnest/node_modules/#nestjs/core/exceptions/exceptions-handler.js:31:26)
at ExceptionsHandler.next (/Users/mapoferl/Documents/develop/BuergermeisterApp/backendnest/node_modules/#nestjs/core/exceptions/exceptions-handler.js:12:18)
at /Users/mapoferl/Documents/develop/BuergermeisterApp/backendnest/node_modules/#nestjs/core/router/router-proxy.js:12:35
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:35014) 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:35014) [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.
d.service.ts
public async getCorporateDesignByDistrictName(name: string): Promise<CorporateDesign> {
const district: District = await this.districtRepository.createQueryBuilder('district')
.leftJoinAndSelect('district.mandant', 'mandant')
.leftJoinAndSelect('district.corporateDesign', 'corporateDesign')
.where('district.name = :name')
.andWhere('district.isDeleted = :isDeleted')
.setParameters({
name: name,
isDeleted: false
})
.getOne();
return district.corporateDesign;
}
d.controller.ts
#UseGuards(AuthGuard)
#Get(':name')
public async getCorporateDesignByDistrictName(#Param('name') name: string): Promise<CorporateDesign> {
return this.districtService.getCorporateDesignByDistrictName(name);
}
I'm using angular 8 and nest.js

Resources