1

Please explain me how to read this NodeJS docs fs.readFile(file[, options], callback)

data is the contents of the file.

What is the "contents"?!

In strongly typed language like C++ or Java when I need to get know info about some method argument or return value e.g. System.getProperty which has argument named key and its type (as we can really easily see from the method signature) is String.

  1. I open a class (in our case it's System) in JDK documentation and scroll to a method I interested in.
  2. If I need to know what can I do with the argument or return value (I mean what the methods the argument has and etc.) I simply click on the class String and goto step 1.

So, my question: I'm writing

var fs = require('fs')
fs.readFile(someFile, 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result = data.???
  // is  data.replace(/abc/g, 'xyz') OK? what else???

});

How can I determine the type(do I need the type?) of the data object (first of all I need the list of methods)?

How I supposed to know that the data is probably String consequently it has method replace?

PS: Obviously helpless console.log(typeof data); says it is just an object.

1 Answer 1

1

They are not as good as the JavaDocs. If you read through a bit you'll find what you are looking for. They don't have links set up for the return object - I think they should since they have it for the function arguments.

From the docs:

If no encoding is specified, then the raw buffer is returned.

https://nodejs.org/api/buffer.html#buffer_class_buffer

If options is a string, then it specifies the encoding.

Then further down:

If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type

To answer your question: You know it a string because you specified an encoding. If you never you would have a buffer.

When I called 'type of' on my result it returned a String or an Object for Buffer.

Here is some output:

    fs.readFile('c:\\test.txt','utf-8',function (err,data) {  
    console.log(data); }
    );

��T e s t
T e x t
H e l l o
W o r l d !

fs.readFile('c:\\test.txt',null,function (err,data) {  
console.log(data); 
});

Buffer ff fe 54 00 65 00 73 00 74 00 0d 00 0a 00 54 00 65 00 78 00 74 00 0d 0 0 0a 00 48 00 65 00 6c 00 6c 00 6f 00 0d 00 0a 00 57 00 6f 00 72 00 6c 00 64 00 ...

 fs.readFile('c:\\test.txt','utf-8',function (err,data) { 
 console.log(typeof data); 
});

string

 fs.readFile('c:\\test.txt',null,function (err,data) {  
  console.log(typeof data);  });

object

You can also use instanceof:

 fs.readFile('c:\\test.txt',null,function (err,data) {  
console.log(data instanceof Buffer); 
});

true

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.