I am using request NPM package and request take two parameters
request (URL, callback);
Here I somehow want to pass extra argument to my callback function how do I do it.
This is the code I am trying to write
function extractIssues(url,filepath){
request(url,issueCb);
}
function issueCb(err,response,html)
{
if(err){
console.log(err+"at line 61");
}
else{
extractIssueData(html);
}
}
function extractIssueData(html){
let selTool = cheerio.load(html);
let arr = [];
let issueLinksArr = selTool(".flex-auto.min-width-0.p-2.pr-3.pr-md-2 > a");
let result="";
for(let i = 0;i<issueLinksArr.length;i++){
let issueLink = selTool(issueLinksArr[i]).attr("href");
let content = selTool(issueLinksArr[i]).text().trim().split("/n")[0];
let obj = {
link :issueLink,
content:content
}
let str = JSON.stringify(obj);
result = result + str + " ,"+ "\n" ;
}
console.log(result);
}
I want to use filepath in extractIssueData so I need to first catch it in issueCb how do I do it
I can't find proper answer.
This issue disappears entirely if you simplify to use a single function! The following should be equivalent to, if not an improvement on, your code:
let extractIssues = async (url, filepath) => {
// Wait for the html content of a `request` call
let html = await new Promise((resolve, reject) => {
// If an Error occurs reject with the error, otherwise resolve with
// html data
request(url, (err, res, html) => err ? reject(err) : resolve(html));
});
let selTool = cheerio.load(html);
// Collect href and text content values from <a> elements
let resultsArr = selTool(".flex-auto.min-width-0.p-2.pr-3.pr-md-2 > a")
.map(issueLink => ({
link: selTool(issueLink).attr("href"),
content: selTool(issueLink).text().trim().split("/n")[0]
}));
// Print the array of link+content data
console.log(resultsArr);
};
Note that within this function you have access to filepath, so you can use it as is necessary.
A solution without async looks like:
let extractIssues = (url, filepath) => {
request(url, (err, res, html) => {
if (err) throw err;
let selTool = cheerio.load(html);
// Collect href and text content values from <a> elements
let resultsArr = selTool(".flex-auto.min-width-0.p-2.pr-3.pr-md-2 > a")
.map(issueLink => ({
link: selTool(issueLink).attr("href"),
content: selTool(issueLink).text().trim().split("/n")[0]
}));
// Print the array of link+content data
console.log(resultsArr);
});
};
Once again this creates a scenario where filepath is available to be used as necessary within the function.
Note: If you are being "given instruction" not to use async you are being done a disservice; async is a core feature and major selling point of javascript. Once you become comfortable with async you will quickly regret ever having worked without it. I recommend you take the time to understand it! :)
I can create you an easy sample to use callback functions.
It's a stupid example I know but it helps you to understand how callbacks are working.
function myFunc(callback) {
let errMessage = "This is error callback";
let message = "This is normal message";
callback(errMessage, message);
}
function myCallbackFunc(err, result) {
if(!err) {
console.log(result);
return;
}
console.log(err);
}
myFunc(myCallbackFunc);
You won't use it like this but If you want to send a error callback, you should null to response param. Like this:
function myFunc(callback) {
let errMessage = "This is error callback";
let message = "This is normal message";
let somethingBadHappened = true;
if(somethingBadHappened) {
return callback(errMessage, null);
}
callback(null, message)
}
function myCallbackFunc(err, result) {
if(!err) {
console.log(result);
return;
}
console.log(err);
}
myFunc(myCallbackFunc);
Related
I using following code, but some URL from URL List, get 500 Error because of structure of the page.
I get the error exactly on .map((htmlOnePage, index) => line when some URLs page not valid, and the flow control of program goes at Catch Part. How I can find which URL is invalid?
const requestPromise = require('request-promise');
const Promise = require('bluebird');
const cheerio = require('cheerio');
for (var i = 1; i <= 250; i++) {
p = "https://mywebsite.com/" + i.toString()
urls[i - 1] = p
}
Promise.map(urls, requestPromise)
.map((htmlOnePage, index) => {
const $ = cheerio.load(htmlOnePage);
$('.txtSearch1').each(function() {
var h = "";
h = $(this).text()
h = h.replace(/(\r\n|\n|\r)/gm, "")
html44.push(h)
})
shareTuple[urls[index]] = html44;
html44 = []
fs.writeFileSync("data.json", JSON.stringify(shareTuple))
}, {
concurrency: 1
})
.then()
.catch((e) => console.log('We encountered an error' + e));
in other word how I can show which URL get into catch?
You should add try catch phrases inside all iterating functions to pin point the problem. and do the logging for every one based on op that they are catching.
For example i would wrap it like this:
try {
const $ = cheerio.load(htmlOnePage);
$('.txtSearch1').each(function () {
try {
var h="";
h=$(this).text()
h= h.replace(/(\r\n|\n|\r)/gm, "")
html44.push (h)
}catch (error) {
console.log('Error in getting p text');
console.log(error);
}
} catch (error) {
console.log('Error in loading'+ htmlOnePage);
console.log(error);
}
You can break down the error more by looking up through error object values to find the problem if you want to manualy remove it.
The other way to programaticaly remove it is to try doing the request for the page after creating it and also wrapping it in try catch. If it throws an exception you can add in catch to remove it from the list.
That text before console log of error is just so you can see where it broke.
Use a wrapper around requestPromise that catches the error. Note, the return undefined is not really needed. It's just for clarification, that in case of an error nothing is returned.
const requestPromise = require('request-promise');
....
const noThrowRequest = async (url) => {
try {
return await requestPromise(url);
} catch (e) {
return undefined;
}
}
Or if you prefer .then().catch() you can do it as follows
const noThrowRequest = (url) => {
return requestPromise(url)
.then(result => { return result; })
.catch(e => { return undefined; });
}
And then use that wrapper instead of requestPromise and check whether the current result valid or not. I don't know what you want to do in case of an invalid result, so I just return from the callback without any further ado. Adapt that if necessary.
Promise.map(urls, noThrowRequest)
.map((htmlOnePage, index) => {
if (htmlOnePage === undefined) {
console.log(`there was an error at index ${index}`);
return; //stop callback for erronous indexes.
}
...
}
I am trying to implement an await function which local variable I am trying to access outside the function. However, I'm not getting the value which is defined inside function.
async CreateProduceRateAsset(data, callback) {
// Create a new file system based wallet for managing identities.
try {
var TimeStamp = new Date();
var TxId = '';
var blockNumber = '';
const result = await contract.submitTransaction('CreateProduceRateAsset', args);
await network.addBlockListener('block-listener', (err, block) => {
if (err) {
console.log(err);
return;
}
TimeStamp = block.data.data[0].payload.header.channel_header.timestamp;
var Tx_id = block.data.data[0].payload.header.channel_header.tx_id;
var BlockNO = block.header.number;
console.log('TxId', Tx_id)
console.log('blockNumber', BlockNO)
TxId = Tx_id
blockNumber = BlockNO
return TimeStamp, Tx_id, BlockNO
});
console.log('Timestamp', TimeStamp)
console.log('TxId 123', TxId)
console.log('blockNumber 123', blockNumber)
response.data = result
return callback(response);
} catch (error) {
response.httpstatus = 404;
response.message = `Failed to get MVP Price ${error.message} `;
return callback(response);
}
};
In the above code, I want to access Tx_id, Timestamp and BlockNO. For that, I am assigning a local variable to a global variable, however, I'm still getting the value of those as blank.
Could someone help me to get those values?
if I understand network.addBlockListener is a callback-based API and not return a promise so you cant await it, I guessed that you use https://hyperledger.github.io/fabric-sdk-node/release-1.4/module-fabric-network.Network.html
Here is your code with my comments
async CreateProduceRateAsset(data, callback) {
// Create a new file system based wallet for managing identities.
try {
var TimeStamp = new Date();
var TxId = '';
var blockNumber = '';
const result = await contract.submitTransaction('CreateProduceRateAsset', args);
// --> this api is callback based and wont not return a promise so you cant await it
await network.addBlockListener('block-listener', (err, block) => {
if (err) {
console.log(err);
return;
}
TimeStamp = block.data.data[0].payload.header.channel_header.timestamp;
// --> becasue you are inside a function these vars are not the same as the one ouside the call back , becasue they are scoped to this function
var Tx_id = block.data.data[0].payload.header.channel_header.tx_id;
var BlockNO = block.header.number;
console.log('###########TxId#####################', Tx_id)
console.log('###########blockNumber#####################', BlockNO)
TxId = Tx_id
blockNumber = BlockNO
// --> you can only return one value in js, also this reurn is useless since you dont call this callback
// --> better way to get these vars outside the callback is to pass them to your own callbak like this `callback(response,TimeStamp, Tx_id, BlockNO)`
return TimeStamp, Tx_id, BlockNO
});
console.log('*************** Timestamp:: **********************', TimeStamp)
console.log('###########TxId#####################123', TxId)
console.log('###########blockNumber#####################123', blockNumber)
response.data = result
return callback(response);
} catch (error) {
// if(error) throw error;
// response.error = error;
response.httpstatus = 404;
response.message = `Failed to get MVP Price ${error.message} `;
return callback(response);
}
};
if you need to use async/await with network.addBlockListener you can use utils.promisify to convert it to promise-based API but I don't recommend it since this an event listener
You have to put your return into a variable like this:
var data = await network.addBlockListener('block-listener', (err, block) => {
if (err) {
console.log(err);
return;
}
TimeStamp = block.data.data[0].payload.header.channel_header.timestamp;
var Tx_id = block.data.data[0].payload.header.channel_header.tx_id;
var BlockNO = block.header.number;
console.log('###########TxId#####################', Tx_id)
console.log('###########blockNumber#####################', BlockNO)
TxId = Tx_id
blockNumber = BlockNO
return {TimeStamp, Tx_id, BlockNO};
});
console.log('*************** Timestamp:: **********************', data.TimeStamp)
console.log('###########TxId#####################123', data.Tx_id)
console.log('###########blockNumber#####################123', data.BlockNO)
Now you can store all your values into data and put them into the desired variables.
I'm trying to create my own wrapper for mysql for my nodejs application. I have two questions here one of which theres a work around and one where I'm unsure what to do as my javascript skills are still in the learning phase.
First thing: As of right now when you navigate to /api/finance it directs you to the finance controller and the index method. This is currently just for testing purposes trying to figure out how to this kind of stuff.
FinanceController:
const sql = require('../../sql.js')
module.exports = {
index: (req, res, next) => {
sql.get('test').then((result) => {
res.status(200).json(result);
})
}
}
sql.js
const mysql = require('mysql');
const { DB } = require('./config')
var connection = mysql.createConnection(DB)
module.exports = {
get: function(table, columns = '*') {
return new Promise((resolve, reject) => {
connection.query('SELECT ?? FROM ?? ', [columns, table], function (error, results, fields) {
if (error) throw error;
resolve(results);
});
})
},
all: function(table) {
return new Promise((resolve, reject) => {
connection.query('SELECT * FROM ?? ', table, function (error, results, fields) {
if (error) throw error;
resolve(results);
});
})
},
where: function(){
console.log('where called')
}
}
As you can see, I have a get() and all(). get() allows you to pass the table name and an array of columns for example: ['id', 'name'] would get you the id column and name column. columns = '*' was an attempt on being able to use one function to either get all columns of the table or specify specific columns however it returns an error: Unknown column in 'field list' so all() was my "workaround" however i'd like it to be one function.
Next I can't figure out how to stack/pipe methods? if thats the word.
The goal here would be so I could call the function like this:
index: (req, res, next) => {
sql.all('test').where().then((result) => {
res.status(200).json(result);
})
}
}
obviously within the .where() I would have it like: .where('id', '=', 'userID') or something along those lines.
however I'm unsure on how to go about doing that and would like some guidance if its possible. I receive the error: sql.all(...).where is not a function
Instead of immediately launching the SQL, you should simply register the provided information in an instance object (having the same methods) and return that object, and let each method enrich the SQL until the end of the chain is reached and you call a method that will launch the SQL.
The object that is passed from one method to the next (as this) maintains state, and collects the different elements of the SQL statement.
Here is one way to do it.
NB: In this demo I used a mock-object for connection. This mock object will not actually connect to a database. Its query method will just produce the final SQL (with all ? resolved) instead of a real data set.
// Mock connection object for this snippet only. Just produces the final SQL, not the result
var connection = {
query(sql, params, callback) {
let i = 0;
sql = sql.replace(/\?\??/g, (m) => {
if (m.length > 1) return [].concat(params[i++]).map(p => "`" + p + "`").join(", ");
if (typeof params[i] === "string") return "'" + params[i++].replace(/'/g, "''") + "'";
return params[i++];
});
setTimeout(callback(null, sql));
}
}
// Function to create an instance when a first method call is made on the `sql` object directly
// Chained method calls will keep using that instance
function getInstance(inst) {
if (inst.columns) return inst; // Keep using same instance in the chain
inst = Object.create(inst); // No instance yet: create one
inst.table = null;
inst.params = [];
inst.columns = [];
inst.conditions = [];
inst.order = [];
return inst;
}
// This sql object serves a starting point as well
// as proto object for the instance object that
// passes through the chain:
var sql = {
from(table) {
let inst = getInstance(this);
inst.table = table;
return inst;
},
select(...columns) {
let inst = getInstance(this);
inst.columns = inst.columns.concat(columns);
return inst;
},
where(column, cmp, value) {
if (!["<",">","<=",">=","=","!="].includes(cmp)) throw "invalid operator";
let inst = getInstance(this);
inst.params.push(column, value);
inst.conditions.push(cmp);
return inst;
},
orderBy(...columns) {
let inst = getInstance(this);
inst.order = inst.order.concat(columns);
return inst;
},
promise() {
if (!this.table) throw "no table specified";
// build SQL and parameter list
let sql = "SELECT *";
let params = [];
if (this.columns.length && this.columns != "*") {
sql = "SELECT ??";
params.push(this.columns);
}
sql += " FROM ??";
params.push(this.table);
if (this.conditions.length) {
sql += " WHERE " + this.conditions.map(cmp => `?? ${cmp} ?`).join(" AND ");
params.push(...this.params);
}
if (this.order.length) {
sql += " ORDER BY ??";
params.push(this.order);
}
return new Promise(resolve => {
connection.query(sql, params, function (error, results) {
if (error) throw error;
resolve(results);
});
});
}
};
// demo
sql.from("customer")
.select("id", "name")
.where("name", ">", "john")
.where("name", "<", "thomas")
.orderBy("name", "id")
.promise()
.then(console.log);
Note that in this implementation it does not matter in which order you chain the from, select, where and order method calls. You could even do the following if you wanted to:
sql .orderBy("name", "id")
.where("name", ">", "john")
.from("customer")
.where("name", "<", "thomas")
.select("id", "name")
.promise()
.then(console.log);
I'm new to learning Node.js, so I'm still getting used to asynchronous programming and callbacks. I'm trying to insert a record into a MS SQL Server database and return the new row's ID to my view.
The mssql query is working correctly when printed to console.log. My problem is not knowing how to properly return the data.
Here is my mssql query - in addJob.js:
var config = require('../../db/config');
async function addJob(title) {
var sql = require('mssql');
const pool = new sql.ConnectionPool(config);
var conn = pool;
let sqlResult = '';
let jobID = '';
conn.connect().then(function () {
var req = new sql.Request(conn);
req.query(`INSERT INTO Jobs (Title, ActiveJD) VALUES ('${title}', 0) ; SELECT ##IDENTITY AS JobID`).then(function (result) {
jobID = result['recordset'][0]['JobID'];
conn.close();
//This prints the correct value
console.log('jobID: ' + jobID);
}).catch(function (err) {
console.log('Unable to add job: ' + err);
conn.close();
});
}).catch(function (err) {
console.log('Unable to connect to SQL: ' + err);
});
// This prints a blank
console.log('jobID second test: ' + jobID)
return jobID;
}
module.exports = addJob;
This is my front end where a modal box is taking in a string and passing it to the above query. I want it to then receive the query's returned value and redirect to another page.
// ADD NEW JOB
$("#navButton_new").on(ace.click_event, function() {
bootbox.prompt("New Job Title", function(result) {
if (result != null) {
var job = {};
job.title = result;
$.ajax({
type: 'POST',
data: JSON.stringify(job),
contentType: 'application/json',
url: 'jds/addJob',
success: function(data) {
// this just prints that data is an object. Is that because I'm returning a promise? How would I unpack that here?
console.log('in success:' + data);
// I want to use the returned value here for a page redirect
//window.location.href = "jds/edit/?jobID=" + data;
return false;
},
error: function(err){
console.log('Unable to add job: ' + err);
}
});
} else {
}
});
});
And finally here is the express router code calling the function:
const express = require('express');
//....
const app = express();
//....
app.post('/jds/addJob', function(req, res){
let dataJSON = JSON.stringify(req.body)
let parsedData = JSON.parse(dataJSON);
const addJob = require("../models/jds/addJob");
let statusResult = addJob(parsedData.title);
statusResult.then(result => {
res.send(req.body);
});
});
I've been reading up on promises and trying to figure out what needs to change here, but I'm having no luck. Can anyone provide any tips?
You need to actually return a value from your function for things to work. Due to having nested Promises you need a couple returns here. One of the core features of promises is if you return a Promise it participates in the calling Promise chain.
So change the following lines
jobID = result['recordset'][0]['JobID'];
to
return result['recordset'][0]['JobID']
and
req.query(`INSERT INTO Jobs (Title, ActiveJD) VALUES ('${title}', 0) ; SELECT ##IDENTITY AS JobID`).then(function (result) {
to
return req.query(`INSERT INTO Jobs (Title, ActiveJD) VALUES ('${title}', 0) ; SELECT ##IDENTITY AS JobID`).then(function (result) {
and
conn.connect().then(function () {
to
return conn.connect().then(function () {
You may need to move code around that is now after the return. You would also be well served moving conn.close() into a single .finally on the end of the connect chain.
I recommend writing a test that you can use to play around with things until you get it right.
const jobId = await addJob(...)
console.log(jobId)
Alternatively rewrite the code to use await instead of .then() calls.
so for the discord bot that im using, I'm able to create a custom playlist, and save it as a txt file. I want to read from the text file and queue up each song in it. The problem is thats it's an async function to do it so I lose order even though i want it to stay in order, would there be any way to go about this? This is what my code looks like and I'm not sure where to start to change it to make it keep order from first song to last. The playlist filelooks like this:
thunder
believer
meant to be
country roads take me home
closer remix
mean to me
And the code looks like this. Any help would be appreciated. Thanks!
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(actualPlaylist)
});
lineReader.on('line', async function(line){
console.log('Line from file:', line);
url = line ? line.replace(/<(.+)>/i, '$1') : '';
console.log(url);
try{
var aVideo = await youtube.getVideo(url);
} catch(error) {
try{
var myVideos = await youtube.searchVideos(line, 1);
var aVideo = await youtube.getVideoByID(myVideos[0].id);
} catch(myError) {
console.error(myError);
return msg.channel.send("Couldnt find any videos by that name.");
}
}
return handleVideo(aVideo, msg, voiceChannel, false); // This is a async function we create down below next to the 'play' function!
});
One way to achieve this would be to make a queue and have it unshift the songs when it started the next one. Something like this maybe. Note this code is untested. Good Luck!
/* globals youtube msg handleVideo voiceChannel */
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('./playlist.txt')
})
let songList = []
let called = false
async function getSong (item) {
let aVideo
try {
aVideo = await youtube.getVideo(item.url)
} catch (error) {
try {
const myVideos = await youtube.searchVideos(item.line, 1)
aVideo = await youtube.getVideoByID(myVideos[0].id)
} catch (myError) {
console.error(myError)
return msg.channel.send('Couldnt find any videos by that name.')
}
}
return handleVideo(aVideo, msg, voiceChannel, false)
}
function videoQueue () {
if (!songList.length) {
called = false
return
}
const item = songList.unshift()
getSong(item)
.then(() => {
videoQueue()
})
.catch(() => {
console.log('trying to play next song')
videoQueue()
})
}
lineReader.on('line', async function (line) {
console.log('Line from file:', line)
const url = line ? line.replace(/<(.+)>/i, '$1') : ''
songList.push({url, line}) // {url, line} = {url:url, line:line}
if (!called) videoQueue()
})