0

I have 2 questions about the code below I wrote to save the data om JSON returned by some REST services

Problem 1

I have a kind of problem before writing to files the data returned by some REST services. The problem is not calling the rest service and getting the data. But just before it writes to the directory

When I call the function , in the trace there's a error " Error: EISDIR: illegal operation on a directory, open '/home/inttyl/data/'"

The files are all correctly saved in the directory I dont understand why there`s an error.

Problem 2

The second problem is the output,

I was expecting the whole path+ filename

SUCCES writing to file:: /home/inttyl/data/file1.js

and not only the path

SUCCES writing to file:: /home/inttyl/data/

Thanks a lot for your help

Here the code

services.forEach(function(currVal, index, array) {

    fullUrl = url + currVal.path;
    fullPath = dataDirectory + currVal.fileName;

    console.log(" Processing writing to file:: " + fullPath);

    request
        .get(fullUrl)
        .on("error", function(err) {
            console.log(err);
        })
        .pipe(fs.createWriteStream(fullPath))
        .on("error", function(err) {
            console.log(" ERROR : wirting in file : " + fullPath);
            console.log("        " + err);

        })
        .on("finish", function() {
            console.log("  Successfully write to file:  " +  fullPath);
        });

});

Here the traces I got :

 Processing writing to file:: /home/inttyl/data/file1.js
 Processing writing to file:: /home/inttyl/data/file2.js
 Processing writing to file:: /home/inttyl/data/file3.js
 Processing writing to file:: /home/inttyl/data/fil4.js
 Processing writing to file:: /home/inttyl/data/fil5.js
 Processing writing to file:: /home/inttyl/data/file6.js
 Processing writing to file:: /home/inttyl/data/file7.js
 Processing writing to file:: /home/inttyl/data/fil8.js

 ERROR : writing to the file  :/home/inttyl/data/
        Error: EISDIR: illegal operation on a directory, open '/home/inttyl/data/'
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js
  SUCCES writing to file:: /home/inttyl/data/fil8.js

EDIT

Problem 1 solved, my bad because in the data, there one fileName attritue that is invalid.

For problem 2, I updates the trace and it always print the last filename. Do you have an idea how to solve it ? it is a problem with closure but I dont known how to do it .

inttyl
  • 111
  • 1
  • 1
  • 8
  • The problem is not within the code presented, but the input given by `currVal` seems to be wrong. – Sirko Sep 14 '16 at 14:59
  • 2
    Does the `services` array have a `.` or `..` filename entry in it or an entry with a blank filename? I'd suggest you show a `console.log()` of the services array. – jfriend00 Sep 14 '16 at 15:00
  • @Sirko , indeed in my data , theres an entry that had file name is empty !!! – inttyl Sep 14 '16 at 17:14
  • @jfriend00 , thanks , my bad, the data is missing a filename ! – inttyl Sep 14 '16 at 17:14

1 Answers1

0

Problem 1

Take a look at the last message of your "Processing" log:

Processing writing to file:: /home/inttyl/data/

It does not contain a file like the others:

Processing writing to file:: /home/inttyl/data/file1.js

So the process is trying to write to a dir instead of a file, and that is the cause of Error: EISDIR

At this function, you probably want to make sure that currVal.fileName is valid before the processing.

Problem 2

Declare your variables with let or const.

e.g. const fullPath = dataDirectory + currVal.fileName;

It was indeed a problem of closure/scope. You weren't declaring your variables, even with var, which makes them to be set at the global scope. So, for each step in the loop you were assigning the value to the same variable. "Later", at the "finish" of the first writefile, it would be logging the last value assigned to fullPath.

By declaring the variable, each loop is now handling its own set of variables.

Tip: go with 'use strict'. It will force you to declare you variables.

Community
  • 1
  • 1
Diego ZoracKy
  • 2,227
  • 15
  • 14
  • Indeed the filnename attribut of one entry in my array is not valid – inttyl Sep 14 '16 at 17:15
  • @inttyl yeah i thought that something like that would be the case. – Diego ZoracKy Sep 14 '16 at 17:21
  • thansk, have an idea for problem 2, I updated the trace and it always print the last filename – inttyl Sep 14 '16 at 17:23
  • @inttyl what is the version of Node.js that you are working with? – Diego ZoracKy Sep 14 '16 at 17:24
  • @inttyl declare your variables with `let` or `const`. e.g. `const fullPath = dataDirectory + currVal.fileName;` – Diego ZoracKy Sep 14 '16 at 17:52
  • it works, but I dont understand why. Could you explain. I thought it was a problem of closure. [javascript-closure-inside-loops-simple-practical-example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – inttyl Sep 14 '16 at 18:20