Converting Object Promise to String in Js - node.js

const url = 'https://www.benzinga.com/stock/aapl/';
const url1 = 'https://www.benzinga.com/stock/msft/';
const url2 = 'https://www.benzinga.com/stock/mu/';
axios.all([
axios.get(url),
axios.get(url1),
axios.get(url2)
])
.then(axios.spread((url11, url12, url13) => {
async function scraper() {
try{
var benzinga1 = url11;
return benzinga1;
} catch (err) {
console.error(err);
}
};
scraper();
async function scraper1() {
try{
var benzinga2 = url12;
return benzinga2;
} catch (err) {
console.error(err);
}
};
scraper1();
async function scraper2() {
try{
var benzinga3 = url13;
return benzinga3;
} catch (err) {
console.error(err);
}
};
scraper2();
async function final() {
try{
console.log(mybenzinga1);
} catch (err) {
console.error(err);
}
};
final();
}))
.catch((error) => {
next(error);
});
when saving url11 to the file it writes [Object object] how to derive a line from a promise.
I can not understand how to convert to a string and save the value to a file
when saving url11 to the file it writes [Object object] how to derive a line from a promise.
I can not understand how to convert to a string and save the value to a file

Try This
const axios = require("axios");
const fs = require('fs').promises;
const url = 'https://www.benzinga.com/stock/aapl/';
const url1 = 'https://www.benzinga.com/stock/msft/';
const url2 = 'https://www.benzinga.com/stock/mu/';
axios.all([
axios.get(url),
axios.get(url1),
axios.get(url2)
]).then((data) => {
data.map(async (item, index) => {
try {
await fs.writeFile(`index${index}.html`, item.data);
} catch (e) {
console.log(e);
}
});
}).catch((error) => {
next(error);
});

Related

Node.js MS SQL transaction

Can anyone help to implement MS SQL transactions in Node.js . I am try to execute multiple stored procedures inside a promise.
Method 1
const executeProcedure = async (data1, data2) => {
try {
// sql connection
let dbConn = new sql.ConnectionPool(config));
await dbConn.connect();
let transaction = new sql.Transaction(dbConn);
await transaction.begin().then(async()=> {
// tranaciton create
// begin tran
let result = await insertOperation(transaction, data1);
let result2 = await updateOperation(transaction, data2);
let result1 = await Promise.all([result, result2]);
await transaction.commit();
dbConn.close();
}).catch(async(err)=> {
await transaction.rollback();
dbConn.close();
throw err;
});
return {};
}
catch (error) {
throw(error);
}
}
method 2
const insertOperation = async (transaction,data1) => {
return new Promise((resolve, reject) => {
try {
var request = new sql.Request(transaction);
request.input('data1' , sql.NVarChar(40) , data1)
.execute('dbo.insertOperation').then((recordSet) => {
resolve(recordSet.recordsets);
}).catch((err) => {
reject(err);
});
}
catch (error) {
reject(error);
}
});
}
method 3
const updateOperation = async (transaction,data2) => {
return new Promise((resolve, reject) => {
try {
var request = new sql.Request(transaction);
request.input('data2' , sql.NVarChar(40) , data2)
.execute('dbo.updateOperation').then((recordSet) => {
resolve(recordSet.recordsets);
}).catch((err) => {
reject(err);
});
}
catch (error) {
reject(error);
}
});
}
Now I get this error
Can't rollback transaction. There is a request in progress.
anybody please help me to solve this problem
You make some unnecessary Promise wrapper.
Example below:
const insertOperation = async (request, data1) => {
request.input("data1", sql.NVarChar(40), data1);
const result = await request.execute("dbo.insertOperation");
return result.recordsets;
};
const updateOperation = async (request, data2) => {
request.input("data2", sql.NVarChar(40), data2);
const result = await request.execute("dbo.updateOperation");
return result.recordsets;
};
const executeProcedure = async (data1, data2) => {
// sql connection
const dbConn = new sql.ConnectionPool(config);
await dbConn.connect();
let transaction;
try {
transaction = new sql.Transaction(dbConn);
await transaction.begin();
const request = new sql.Request(transaction);
const results = await Promise.all([
insertOperation(request, data1),
updateOperation(request, data2),
]);
await transaction.commit();
return results;
} catch (err) {
await transaction.rollback();
throw err;
} finally {
await dbConn.close();
}
};
#ikhvjs please check the below use case as well
try {
request.input("data", sql.NVarChar(40), data1);
const result = await request.execute("dbo.insertOperation");
return result.recordsets;
} catch (err) {
throw err;
}
};
const updateOperation = async (request, data2) => {
try {
request.input("data", sql.NVarChar(40), data2);
const result = await request.execute("dbo.updateOperation");
return result.recordsets;
} catch (err) {
throw err;
}
};
const executeProcedure = async (data1, data2) => {
try {
// sql connection
const dbConn = new sql.ConnectionPool(config);
await dbConn.connect();
const transaction = new sql.Transaction(dbConn);
try {
await transaction.begin();
const request = new sql.Request(transaction);
const results = await Promise.all([
insertOperation(request, data1),
updateOperation(request, data2),
]);
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
} finally {
await dbConn.close();
}
} catch (error) {
throw error;
}
};```

problem in sending base64 data in GET request

Hi I am facing issues sending base64 data in GET request.
I was successful in converting the image into base64 data and inserting it in receivedFile
but during response the attachments come as an empty array while the rest of the data i.e user_id is flowing successfully.
Hence if you could please help me to resolve this issue.
Below is the code
router.js
router.get('/users/data/expand/:nid',async (req,res) => {
var idselected = req.params.nid;
var dir = '\images';
var receivedFile = [];
try {
const checkData = await user.find({"user_id": idselected});
await checkData[0].attachments.forEach (element => {
fs.readdir(dir,function(err,files) {
if(err) {
console.log(err)
}else {
files.forEach((filename) => {
filename = element;
fs.readFile(filename,'base64', (err,base64Data) => {
if(err) {
console.log(err);
}
receivedFile.push(base64Data);
})
})
}
})
})
//issue is here the attachments is coming as empty instead of base64 data
const returnUser = new User({
user_id: checkData.user_id,
attachments: receivedFile
})
res.status(201).send(returnUser);
}
catch(e) {
res.status(500).send(e)
}
})
Well its always good to create helper functions and to promisfy it so you can use async / await syntax.
I have changed your code. I didnt tested it but i guess it should work:#
router.get("/users/data/expand/:nid", async (req, res) => {
var idselected = req.params.nid;
var dir = "images";
try {
const checkData = await user.findOne({ user_id: idselected });
let receivedFile = await Promise.all(
checkData.attachments.flatMap(async element => {
let files = await readDirectory(dir);
return await Promise.all(
files.map(filename => {
filename = element;
return readFile(filename)
})
);
})
);
const returnUser = new User({
user_id: checkData.user_id,
attachments: receivedFile
});
let savedUser = await returnUser.save();
res.status(201).send(savedUser);
} catch (e) {
res.status(500).send(e);
}
});
function readDirectory(dir) {
return new Promise((res, rej) => {
fs.readdir(dir, function(err, files) {
if (err) {
rej(err);
} else {
res(files);
}
});
});
}
function readFile(filename) {
return new Promise((res, rej) => {
fs.readFile(filename, "base64", (err, base64Data) => {
if (err) {
rej(err);
}
res(base64Data);
});
});
}
I guess you use mongoose.
There is an method called findOne and also you forgot to save your model with returnUser.save()

Return async function to express router

This may sound dum. But what I am stuck here with is
1: returning the converted JSON file
2: getting the returned object into the route
routes.js
const express = require('express');
const router = express.Router();
const routings = require('./src/services/routings');
router.get('/routings', async(req, res) => {
const routesRes = await routings.getRoutings();
res.end(JSON.stringify(routesRes, null, " ")).catch(function (err) {
console.log(err);
});
});
module.exports = router;
routings.js
const parseXml = require('xml2js')
let data = `<?xml version="1.0" encoding="UTF-8"?>...'
getRoutings = async() => {
await parseXml.parseStringPromise(data).then(function (result) {
console.log('Done');
return result;
})
.catch(function (err) {
console.log(err);
});
}
module.exports = {getRoutings}
Your getRoutings() function does not have a return value. Therefore when you do this:
const routesRes = await routings.getRoutings();
routesRes will always be undefined.
I would suggest this:
getRoutings = () => {
return parseXml.parseStringPromise(data).then(function (result) {
console.log('Done');
return result;
}).catch(function (err) {
console.log(err);
throw err; // make sure error is propagated
});
}

Appending array values to csv

I would like to take some output and append the output to a csv
This is the code I have so far:
async function writeData() {
const csv = require('csv-parser')
const results = [];
fs.createReadStream('C:\\Users\\User\\Documents\\testingclean.csv')
.pipe(csv())
.on('data',(data)=> results.push(data))
.on('end', async () => {
const cookieJar = await getCookieJar();
const promises = [];
results.forEach((data) => {
promises.push(boxrec.getPersonById(cookieJar,data.id));
})
try {
const fighters = await Promise.all(promises);
fighters.forEach((fighter)=>{
boxer = JSON.parse(JSON.stringify(fighter.output));
fs.appendFile('C:\\Users\\User\\Documents\\newtest.csv',boxer, (err) => {
if (err) console.error('Could not append data to csv');
console.log('Data successfully appended');
})
});
} catch (e) {
console.log(e);
}
})
};
try {
writeData();
} catch (error) {
console.log("Error in writeData: " + error);
}
However running this code does not produce the desired csv output.
I am specifically writing to csv because I have read that I cannot append to a json (would ideally want to write data to a json)
If you don't have anything writen to the CSV file, you should close it explicitly at the end of your program :
var readsteam = fs.createReadStream();
...
readStream.destroy();
Not sure but this should be the full code :
async function writeData() {
const csv = require('csv-parser')
const results = [];
var readsteam = fs.createReadStream('C:\\Users\\User\\Documents\\testingclean.csv');
readsteam
.pipe(csv())
.on('data',(data)=> results.push(data))
.on('end', async () => {
const cookieJar = await getCookieJar();
const promises = [];
results.forEach((data) => {
promises.push(boxrec.getPersonById(cookieJar,data.id));
})
try {
const fighters = await Promise.all(promises);
fighters.forEach((fighter)=>{
boxer = JSON.parse(JSON.stringify(fighter.output));
fs.appendFile('C:\\Users\\User\\Documents\\newtest.csv',boxer, (err) => {
if (err) console.error('Could not append data to csv');
console.log('Data successfully appended');
})
});
} catch (e) {
console.log(e);
}
}
readsteam.destroy();
};
try {
writeData();
} catch (error) {
console.log("Error in writeData: " + error);
}

Using async await with setImmediate

I want to know the how to use setImmediate with async await and handle errors properly. I have written following code. But I am not sure it is adhering to the best practices.
There is a route in my express app
router.get('/parseinvoice', async (req, res, next) => {
try {
const parsedInvoiceResponse = await userhelper.getParseInVoiceList();
res.json({parsedInvoiceResponse})
} catch (error) {
res.json({});
}
});
The userhelper class code
var userhelper = {};
const fs = require('fs'),
path = require('path'),
filePath = path.join(__dirname, './input_user_story_12.txt');
const { promisify } = require('util')
const readFile = promisify(fs.readFile);
userhelper.getParseInVoiceList = async function() {
return new Promise( async (resolve, reject) => {
try {
setImmediate(async function() {
try {
const contents = await readFile(filePath, 'UTF-8');
resolve(contents);
} catch (error) {
reject(error);
}
});
} catch (error) {
reject(error);
}
});
}
module.exports = userhelper;
Although I am getting the response. I am not sure about the setImmediate part, whether the multiple try catch are required. Is there any neat way to write the below code?.
try {
setImmediate(async ()=>{
var res = await readFile(filePath, 'UTF-8');
})
} catch(err) {
}
2.
await setImmediate(()=>{
var res = await readFile(filePath, 'UTF-8');
}).catch(){}
3.
try {
await setImmediate(()=>{
await readFile(filePath, 'UTF-8');
}).catch(){}
} catch() {
}
should return result into res
const res = await setImmediate(()=>{
return readFile(filePath, 'UTF-8');
})
Why are you not just using?
userhelper.getParseInVoiceList = async function() {
return await readFile(filePath, 'UTF-8');
}
Expanding on #Dan D.'s answer, you can await the resolution of an asynchronous setImmediate prior to calling the asynchronous promisified readFile, but I am not sure why you would need to do this without more context.
userhelper.getParseInVoiceList = async function() {
await new Promise((resolve) => setImmediate(() => resolve()));
return await readFile(filePath, 'UTF-8');
}

Resources