Home > Mobile >  Why doesn’t node.js get the exact content of my file?
Why doesn’t node.js get the exact content of my file?

Time:07-15

I've started experimenting with node.js because I'm interested in the back-end part of JavaScript. Now, I want to extract the content of a file and check whether it's equal to (what I think) is the content of the file. I've made sure to not have any spaces in my file.

var fs = require('fs');

fs.readFile("datei.txt", "utf-8",function(err, f){
    
    if(f == "hey!"){
        console.log("worked!");
    }else{
        console.log("didn't work");
    }
});

That's the content of my file

CodePudding user response:

You may trim() the strings and compare to see if the issue is whitespace, or encoding. My guess it's whitespace :-) Possibly Unix vs Windows new lines.

CodePudding user response:

Probably you have a new line in the datei.txt. Add trim to compare:

if (f.trim() == "hey!") {

I have tested and worked for me.

  • Related