inttyl inttyl - 2 months ago 14
Node.js Question

Nodejs file writing strange behavior and variable value issue

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 .

Answer

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.