0

I was studying nodejs peacefully. and I was learning about fs.readFile. So for practice, I made a simple code.

var list;
fs.readFile('list.txt', 'utf8', function(err, data){
list=data;
});

but when I got the value of list, it was "undefined".

So, I tried to console data's value like this

var list;
fs.readFile('list.txt', 'utf8', function(err, data){
list=data;
console.log(data);
});

the problem start's now.
I got the error, TypeError: console.log is not a function.

So, I checked on many questions like this or this...

But I could not find the answer to my question.

Why is this error happening, and how can I stop this happening?

the full code is

var http = require('http');
var fs = require('fs');
var url = require('url');
  var printconsole="";
function console(data){
  return "<script>console.log(\""+data+"\");</script>";
}
  var app = http.createServer(function(request,response){
  var template;
    var _url = request.url;
    var queryData = url.parse(_url, true).query;
    var title = queryData.id;
    var main;
    var list;
    fs.readFile('list.txt', 'utf8', function(err, data){
        console.log("data");
        list=data;
    });
    fs.readFile(`${queryData.id}.txt`, 'utf8', function(err, data){ 
        main=data;
    });
    if(_url == '/'){
      title = 'Web';
      main = `world wide web`;
    }
    if(_url == '/favicon.ico'){
      return response.writeHead(404);
    }
    response.writeHead(200);
    template = `
    <!doctype html>
    <html>
    <head>
      <style>
        body{
          width:-webkit-fill-availble;
          height:-webkit-fill-availble;
        }
        header{
          width:100%;
          height:10%;
          border-bottom:1px solid #111111;
        }
        aside{
          width:10%;
          height:560px;
          border-right:1px solid #111111;
          float:left;
        }
        article{
          width:85%;
          height:85%;
        }
      </style>
      <meta charset="utf-8">
    </head>
    <body>
          <header><h1>${title}</h1></header>
          <aside>${list}</aside>
           <article>${main}</article>
        </body>
    </html>
    `;
    response.end(template);
});

app.listen(3000);
  • 2
    Is this the full program? If there's more you're not sharing, then please [edit] and provide a [mcve] because this does _not_ reproduce the problem you're describing. – Patrick Roberts Dec 09 '18 at 08:31
  • @Patrick Roberts edited! Thank's for your help –  Dec 09 '18 at 08:36
  • 1
    Your problem is defining `console` as a function. `console.log()` is already a builtin function, and `console` is a builtin object. If you define that function, then you're hiding the builtin from that scope, which is causing that error to happen. – Patrick Roberts Dec 09 '18 at 08:38

2 Answers2

2
function console(data){
  return "<script>console.log(\""+data+"\");</script>";
}

You overwrote console with your own function, which doesn't have a log property.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • aha.... I forgot that a function's name can not be things like console, for. Thank you for your help –  Dec 09 '18 at 08:39
0

a function's name should not be console,for,if etc.

function console(data){
  return "<script>console.log(\""+data+"\");</script>";
}

is the problem. You should change it's name.

codingboy
  • 114
  • 11