CSVtoJSON returns UnhandledPromiseRejectionWarning: ReferenceError: JavaScript - javascript

I am trying to convert CSV from a CSV file to JSON.
I have the following code
(async() => {
csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
let latest_provinces_confirmed = source;
});
console.log(latest_provinces_confirmed)
...
and when I run it, I get
UnhandledPromiseRejectionWarning: ReferenceError: latest_provinces_confirmed is not defined
How can I get the result of the CSVtoJSON into a variable to use later
Thanks in advance

The variable 'lastest_provinces_confirmed' is declared inside the anonymous function so you cannot access it outside that function. Thats why console.logging variable doesn't work.
You can try to pass that variable outside of that function by returning it OR you can forward declare that variable outside those functions to be able to access it.
Something like this might work:
let latest_provinces_confirmed = csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
return source;
});
Remember to take account that you are working with async functions so you have to make sure that function csvToJson() has run before using 'latest_provinces_confirmed'. You can check use of "await" in order to do that!

You are doing a simple and a beginner level mistake. The scope of "let" is only inside the block. Change your code as below and it should work.
(async() => {
let latest_provinces_confirmed;
csvToJson().fromStream(request.get("https://raw.githubusercontent.com/dsfsi/covid19za/master/data/covid19za_provincial_cumulative_timeline_confirmed.csv"))
.then(source => {
latest_provinces_confirmed = source;
console.log(latest_provinces_confirmed)
....
});

Related

Function's parameter used inside a previously defined variable (String) returns undefined

trying to declare a variable for an API call (There are different website versions for API calls so want to have it dynamically interchangeable while having clean code).
It goes like this:
const getAbi = async (addy) => {
const url = `https://api.arbiscan.io/api?module=contract&action=getabi&address=${addy}&apikey=${arbiscanKey}`;
const abi = await axios.get(url);
console.log(abi);
return abi;
};
Now, its supposed to take the function parameter (Named "addy") and input it inside the API call link. For some reason, it does not do that. It gives me error:
const arbiscanGetAbi = `https://api.arbiscan.io/api?module=contract&action=getabi&address=${addy}&apikey=${arbiscanKey}`;
^
ReferenceError: addy is not defined
Not sure how to get it to realise i am referring to "addy" from the function parameter, thanks!

Can't use variable as field name updateDoc (firebase)

How can i write a variable there? This is React.
${} and ""+"" are not working
Pls i neeed help
const id = 'currentChat[0].
id';
const fn = async () => {
await updateDoc(contactsRef, {
`${id}.chatHistory`: arrayUnion(message),
});};
fn();
If you have an expression, you need to put it inside []. These are called computed property names.
You need to use
[`${id}.chatHistory/`]
Read this.

Puppeteer identifier string variable won't parse; Unsure why

I'm trying to have a string be used inside a puppeteer string, it won't work for some reason.
Specifically with this code
await page.waitForSelector('div[class = "sh-dlr__list-result"')
When i try to parse in a variable
let identified1 = 'div[class = "sh-dlr__list-result"'
so making
await page.waitForSelector(identified1)
It won't work. This is really limiting, is there a way around this issue?
This is the expanded code
https://jsfiddle.net/hewlbern/6p7kdozt/10/
Run it in your computer, jsfiddle unsure if I can run it from there.
I believe it is creating a cors error now - very weird! Why would using a variable create a cors error : /
Thanks!
The reason is because you're declaring identified inside the page.evaluate(). So, when you do the following it's already out of scope.
if (currentPage < pagesToScrape) {
console.log(identified1);
await Promise.all([
await page.click(buttonSelector),
await page.waitForSelector(identified),
]);
}
You did log the identified1 but you're using identified for the selector.
You'll have to pass the identifier2 to the pageFunction like so:
let newProducts = await page.evaluate(({ identifier2 }) => {
// ...
},{ identifier2 });
See here some examples:

How to overcome issue of ".. is not a function" while referring to earlier defined function expression in same js file

On accessing function expression in the same file, gives an error of .. "is not a function".
I need this below function expression to be available both outside the js file to other .js files and as well inside the same js file.
I have tried below things from below blogs, nothing seems to work
https://github.com/nodejs/node/issues/2923
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function
// this is in abc.js
function qpValidations() {
this.addDaystoGetDate = function(noOfDays){
...
}
this.constructDate = function(){
this.addDaystoGetDate(2);// here issue is coming, where trying to
//call function in same .js file
}
}
module.exports = new qpValidations();
Any help is most appreciated!!, though this issue occurred many times with me, tried avoiding file circular dependency and as well clubbing function expression and declaration, had solved the issues earlier, but now again it has poped up not sure what the root cause of this issue..?
If you're only having issues calling the function in the same file, it's hard to help because you haven't shown HOW you're trying to call it. But I would suggest you try something like this:
// this is in abc.js
function qpValidations() {
this.addDaystoGetDate = function(noOfDays){
...
}
this.constructDate = function(){
this.addDaystoGetDate (2);// here issue is coming
}
}
const newQp = new qpValidations()
module.exports = newQp;
newQp.addDaystoGetDate();
addDaystoGetDate function required a parameter. try sending a parameter when you call the function.
newQp.addDaystoGetDate(5);
newQp.addDaystoGetDate("some text"); //or whatever you need
Finally got the answer, it was from one of my buddy:), the scope of this.addDaystoGetDate()
gets changed when it is called inside another this.constructDate() function. So as a solution assign "this" object to some variable like below, further can be referred same wherever we come across this
function validation(){
var refObj = this;
refObj.addDaystoGetDate =function(dayscount){
}
refObj.constructDate = function(){
refObj.addDaystoGetDate(2);
}
}
module.exports = new validtaion();

Meteor variable is not defined

This Meteor server code is complaining
Exception in callback of async function: ReferenceError: myConn is not defined
The variable myConn is defined in ddp.js and is used in method.js,
Any idea why and how to fix it? thx
//server/ddp.js
let myConn = DDP.connect('http://localhost:5000');
//server/method.js
myConn.call('myMethod', {obj}, () => {})
let limits the variable's scope to the block or statement in which it is used.
var defines a variable locally to an entire function or procedure.
To define a variable globally and accesible between multiple files avoid using let or var and just assign a default value to its name. Example:
myVarName="";

Categories

Resources