Hello in my nodejs api i need fetch data inside the loop and then again need to do a loop and save a data in another table how should i achive that?
Here is some snippet that i have tried but not succeeded for the same
async myAPIname(){
let _this = this;
try {
const bets = await Bet.find({gameId:ObjectId(request.matchId)}).lean()
bets.map(async (bet) => {
let Users = await Users.findOne({_id:ObjectId(element.userId)}).lean();
Users.parentTree.map(async (user) => {
console.log(user);
// also over here based on the some calculation need to save the data in another table
})
})
} catch (error) {
_this.res.send({ status: 0, message: error });
}
}
Also in above snipped tried with foreach loop as well but not succeeded
and error from above spinet like this:
(node:30886) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'Users' before initialization
at /var/www/html/api/app/controllers/SomeController.js:228:30
at Array.map (<anonymous>)
at SomeController.myAPIname (/var/www/html/api/app/controllers/SomeController.js:227:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:30886) 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:30886) [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.
(node:30886) UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
at /var/www/html/api/app/controllers/SomeController.js:220:27
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:30886) 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)
Any Help will really appreciated
I can see two problems:
firstly the await inside a map call doesn't work like you think. It will work fine in a for-of loop but not in a map, foreach etc.
https://zellwk.com/blog/async-await-in-loops/ has a good an explanation as anywhere.
Secondly where you are calling let Users = Users.findOne, the compiler thinks that Users on the left hand side of the assignment is the same as the Users on the right hand side so it complains that when its calling Users.findOne, Users isn't initialised.
to use asynchronous handling with Array.prototype.map() you have to wrap it with Promise.all() and wait for it to fulfill.
!! however notice that iterations are executed in asynchronous way, not waiting for previous iteration to settle.
const sleep = async (time = 3000) => new Promise(resolve => setTimeout(resolve, time));
(async () => {
const array = [1,3,4,2];
console.log('start', array);
const mapped =await Promise.all(array.map(async e => {
await sleep(e * 1000);
console.log(e);
return `done for ${e}`;
}));
console.log('end', mapped);
})();
Related
that is my raw response from API call who im trying to get each "bloc" result with a .forEach
const app = express();
const axios = require('axios')
jobList = [];
app.get('/getAPIResponse', function(req, res) {
async function myFunction() {
axios.get('https://api...')
//res.json(body)
.then((result) => {
result.results.forEach((element) => {
console.log(element)
this.jobList.push(element)
});
})
}
myfunction();
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
but the problem is, it made me some error on my terminal
(rejection id: 2)
(node:81826) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at /Users/mac/Desktop/alternatics_test/index.js:22:24
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:81826) 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:81826) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at /Users/mac/Desktop/alternatics_test/index.js:22:24
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:81826) 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: 4)
to be as clear as possible results is the keyword in the api from which I would like to take each result block for insert in my "jobList"
I looked everywhere and the forEach seems to be the right technique for this...
You are using module axios
That will return a response with axios response schema
So you have to get result like this result.data.results then you can use forEach function
use result.data.results.forEach
So I was watching youtube regarding webscraping and would like to give it a go. I tried mimicking exactly how the tutor teaches but I land into a problem right away.
(node:20976) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:261:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext._evaluateInternal (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:161:64)
at async D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:90:30
at async DOMWorld.$x (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:96:26)
at async scrapeProduct (D:\Users\Win81\Desktop\Web-scraper-snkrssg\scraper.js:10:18)
(node:20976) 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:20976) [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.
Here is the sample of my codes:
const puppeteer = require('puppeteer');
async function scrapeProduct(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.goto(url)
const [el] = await page.$x('//*[#id="block-1565393554829"]/div/div/div[2]/div/div[1]/a/div/img[2]');
const src = await el.getProperty('src');
const srcTxt = await src.jsonValue();
console.log({srcTxt});
}
scrapeProduct('https://shopnicekicks.com/pages/calendar');
I hope someone could teach me where I'm going wrong.
I'm using Node.js and Express to build a REST api. The database operations are in a separate file called "db.js". Here's the gist of the code:
...
const db = require('./db');
app.get('/b50api/workitems/me/:pernr', function (req, rs) {
db.getWorkItems("200805").then((items, output)=>{
console.log("ITEMS:", items);
console.log("OUTPUT:", output);
}).catch(err => {
console.log("ERROR:", err);
})
})
...
When I hit this endpoint the program fails with the error below.
I created a test .js file (below) and this runs just fine:
const db = require('./db');
function test() {
db.getWorkItems("200805").then((items, output)=>{
console.log("ITEMS:", items);
console.log("OUTPUT:", output);
}).catch(err => {
console.log("ERROR:", err);
})
}
test();
What am I missing??
Here's the error information when running in my REST api:
(node:14612) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(node:14612) UnhandledPromiseRejectionWarning: TypeError: p.callNotify is not a function
at C:\inetpub\wwwroot\PersonnelApps\b50api\node_modules\msnodesqlv8\lib\procedure.js:332:11
at C:\inetpub\wwwroot\PersonnelApps\b50api\node_modules\msnodesqlv8\lib\procedure.js:305:13
at C:\inetpub\wwwroot\PersonnelApps\b50api\node_modules\msnodesqlv8\lib\procedure.js:194:9
at runNextTicks (internal/process/task_queues.js:62:5)
at processImmediate (internal/timers.js:429:9)
(node:14612) 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:14612) [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.
Turns out the issue was it was attempting to connect to the database as some built-in account. I need to change the site to an application and change the account it's running under to the one that needs to authenticate to the SQL Server (along with making that same account one that can log on as a service).
I have more .chunk files which I fetch using node-fetch and using these files I want to create another file by "gathering them together" somehow.
I tried with this:
var fileStream = await fs.createWriteStream(`./paks/${file.Filename.slice(26)}`)
//below code is in a loop, above isn't in a loop
await fetch(chunkURL)
.then(async res => {
if(chunks.indexOf(chunk) == chunks.length - 1) await res.body.pipe(fileStream, { end: true })
else await res.body.pipe(fileStream, { end: false })
})
but the file is corrupted after it finishes, I also tried with flieStream.write(res.body, { end: true }) instead of res.body.pipe(fileStream, { end: true }) but I get the following error:
(node:12110) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object
at validChunk (_stream_writable.js:265:10)
at WriteStream.Writable.write (_stream_writable.js:300:21)
at /root/api/getUpdate.js:97:87
at processTicksAndRejections (internal/process/task_queues.js:85:5)
at async /root/api/getUpdate.js:94:57
(node:12110) 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: 2)
(node:12110) [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 few queries to a database which I want to gather into a transaction.
let t = await db.sequelize.transaction()
try {
let adr = await addressRepo.add(address, t)
let user = await userRepo.add(email, password, name, surname, phone, NIP, REGON, id_number, adr.id, t)
await userRoleRepo.add(user.id, user_role, t)
if (languages != null) {
languages.forEach(async function (language) {
await userLanguageRepo.add(user.id, language.id, language.main, t)
})
}
await t.commit()
res.status(201).json(user)
} catch (error) {
await t.rollback()
}
According to above code, the transaction is created and all queries are included except of those in forEach loop. As a result I get:
Executing (fb270893-9146-43b7-a35e-8960ea386513): START TRANSACTION;
Executing (fb270893-9146-43b7-a35e-8960ea386513): INSERT INTO `address` (`id`,`country`) VALUES (DEFAULT,'country');
Executing (fb270893-9146-43b7-a35e-8960ea386513): INSERT INTO `user` (`id`,`email`,`password`,`name`,`surname`,`active`,`address_id`,`created_at`,`updated_at`) VALUES (DEFAULT,'a15','$2a$10$7uImQNl0T12CZLUB0Asxwu8yCGUa/eZnbr8TATX8V/tnnO8erdYzy','John','Dee','0',15,'2017-08-28 07:44:03','2017-08-28 07:44:03');
Executing (fb270893-9146-43b7-a35e-8960ea386513): INSERT INTO `user_role` (`user_id`,`role_id`) VALUES (7,1);
Executing (fb270893-9146-43b7-a35e-8960ea386513): COMMIT;
(node:5873) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: commit has been called on this transaction(fb270893-9146-43b7-a35e-8960ea386513), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)
(node:5873) [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.
(node:5873) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: commit has been called on this transaction(fb270893-9146-43b7-a35e-8960ea386513), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)
I looked at async commit is execute before forEach loop. How can I execute forEach loop before commit?
Instead of forEach one should use for of loop.
I found the answer at this post.