-5

I am trying to parse csv file to arrays using this jquery-csv plugin but when checking log for $.csv is says undefined. However, when I copy and execute the code from the plugin directly in the console, $.csv works. I even tried method jquery's getScript() method to import the jquery-csv directly to my custom script but it shows undefined.

$.getScript("../js/jquery-csv.js"); console.log($.csv);

Also tried added this in head and it shows up in Chrome developer tools script(scr='https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.8.9/jquery.csv.min.js') included this in the head of the page. It shows up in head through chrome devtools

P.S - Using pug template engine

Suyash
  • 195
  • 1
  • 1
  • 9
  • 4
    We can't magically know what your attempt to import it looked like, and thus what was wrong with it. Why don't you show us? More: [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Feb 27 '18 at 16:47
  • We need to see some code you used. Help us help you. – justDan Feb 27 '18 at 16:48
  • `$.getScript("../js/jquery-csv.js"); console.log($.csv);` – Suyash Feb 27 '18 at 16:53
  • Why not just use a standard ` – ADyson Feb 27 '18 at 16:53
  • `script(scr='https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.8.9/jquery.csv.min.js')` included this in the head of the page. It shows up in head through chrome devtools – Suyash Feb 27 '18 at 16:56
  • Ok so if you did that you don't need the getScript as well. – ADyson Feb 27 '18 at 16:56
  • When that was not working, I tried getScript() – Suyash Feb 27 '18 at 16:57
  • But that's invalid markup. It should be `` . Not sure where you got that other syntax from? The syntax for script tags is clearly documented in many places online, and/or you can look at the source of any other website to get an example. And also scr should be src – ADyson Feb 27 '18 at 16:58
  • Sorry forgot to mention, I am using pug template engine – Suyash Feb 27 '18 at 16:58
  • Well you still made a typo with scr instead of src. Detail is important. – ADyson Feb 27 '18 at 16:59
  • I changed that but its still undefined. I have been swapping different versions of the plugin hoping this was version related. – Suyash Feb 27 '18 at 17:02
  • @ADyson Could it be because I am using node js and it has other csv parsing modules, like fast-csv, its causing a conflict? If that is the case, what can be done to avoid such conflicts? – Suyash Feb 27 '18 at 17:10
  • I don't understand - if you're loading this into your browser page then nodeJS is not relevant to that - it runs on the server – ADyson Feb 27 '18 at 19:46

1 Answers1

0

You can not use direct $.csv after run getScript function. because maybe it take some time to load js but console.log statement run after getScript statment run . so you have to try this ..

$.getScript("../js/jquery-csv.js",function(){
    console.log($.csv);
});
Bhavik Hirani
  • 1,996
  • 4
  • 28
  • 46
  • That worked! But I still don't understand why adding the script in the head itself doesn't work considering I am waiting for the document to be ready before executing $.csv. – Suyash Feb 27 '18 at 17:24
  • The callback is fired once the script has been loaded but not necessarily executed. – Bhavik Hirani Feb 27 '18 at 17:31