ReferenceError:在javascript中未定义require

时间:2022-11-06 15:03:44

I am html javascript beginner and i simply have to read the contents of a text file at a location and store each column in that file in an array (there are 3 columns) in html and i use javascript to do so.

我是html javascript初学者,我只需要在一个位置读取文本文件的内容,并将该文件中的每一列存储在一个数组(有3列)中,并使用javascript进行操作。

I have came across 2 errors http://prntscr.com/6ilvhx :

我遇到了2个错误http://prntscr.com/6ilvhx:

(1)ReferenceError: require is not defined

(1)ReferenceError:未定义require

(2) ReferenceError: readfile is not defined

(2)ReferenceError:未定义readfile

My code to do this is :

我这样做的代码是:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script>
        console.log("just a check1");
        var fs = require('fs');
        console.log("just a check2");

        function readLines(input, done) {
            var arr = [];
            var remaining = '';

            input.on('data', function (data) {
                remaining += data;
                var index = remaining.indexOf('\n');
                while (index > -1) {
                    var line = remaining.substring(0, index);
                    remaining = remaining.substring(index + 1);
                    func(line);
                    index = remaining.indexOf('\n');
                }
            });

            input.on('end', function () {
                if (remaining.length > 0) {
                    func(remaining);
                    done(arr);
                }
            });

            function func(data) {
                arr.push(data.split(/\s+/g));
            }
        }

        var input = fs.createReadStream('Content\TextFile\distinctGimeiNo.txt');
        readLines(input, done);

        function done(arr) {

            var obj = {};
            var key1 = arr[0][0];
            var key2 = arr[0][1];
            var key3 = arr[0][3];
            obj[key1] = [];
            obj[key2] = [];
            obj[key3] = [];

            arr.shift();

            arr.forEach(function (item) {
                obj[key1].push(item[0]);
                obj[key2].push(item[1]);
                obj[key3].push(item[2]);
            });

            console.log('X:', obj['X']);
            console.log('Y:', obj['Y'])
            console.log('Z:', obj['Z'])
        }
    </script>

</head>
<body>
    <h1>Hello World!</h1>    
</body>
</html>

How to make it work ?I found this on debugging http://prntscr.com/6ilvhx

如何使其工作?我在调试http://prntscr.com/6ilvhx时发现了这一点

Updated code : (after adding library)

更新代码:(添加库后)

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src=http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js>

        console.log("just a check1");
        var fs = require('fs');
        console.log("just a check2");
        function readlines(input, done) {
            var arr = [];
            var remaining = '';
            input.on('data', function (data) {
                remaining += data;
                var index = remaining.indexOf('\n');
                while (index >
        -1) {
                    var line = remaining.substring(0, index);
                    remaining = remaining.substring(index + 1);
                    func(line);
                    index = remaining.indexOf('\n');
                }
            });

            input.on('end', function () {
                if (remaining.length > 0) {
                    func(remaining);
                    done(arr);
                }
            });

            function func(data) {
                arr.push(data.split(/\s+/g));
            }
        }

        var input = fs.createReadStream('Content\TextFile\distinctGimeiNo.txt');
        readLines(input, done);

        function done(arr) {

            var obj = {};
            var key1 = arr[0][0];
            var key2 = arr[0][1];
            var key3 = arr[0][2];
            obj[key1] = [];
            obj[key2] = [];
            obj[key3] = [];

            arr.shift();

            arr.forEach(function (item) {
                obj[key1].push(item[0]);
                obj[key2].push(item[1]);
                obj[key3].push(item[2]);
            });

            console.log('X:', obj['X']);
            console.log('Y:', obj['Y'])
            console.log('Z:', obj['Z'])
        }
    </script>



</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

1 个解决方案

#1


2  

Impossible to use node.js script in your browser, start it with node.. :)

无法在浏览器中使用node.js脚本,请以节点启动.. :)

You can use jQuery for do that in client side :

您可以使用jQuery在客户端执行此操作:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script>
     console.log("just a check1");
     $.ajax({
         type:'GET',
         url:'./hello.txt',
         dataType: 'text',
      }).success(function (data){
               s = data.substr(32);
               var s2 = s.replace(/\r\n/g," ");
               var array = s2.split(' ');

               var col1 =[];
               var col2 =[];
               var col3 =[];
               var j=0;
               for(var i = 0; i <= array.length-3; i=i+3){
                       col1[j]=array[i];
                       col2[j]=array[i+1];
                       col3[j]=array[i+2];
                       j++;
               };

               console.log(col1,col2, col3);
      })
 </script>

</head>
<body>
  <h1>//hello world !</h1>
</body>
</html>

$.ajax allow you to get back datas with a specify url. Normally,that do that you wanted

$ .ajax允许您使用指定的URL返回数据。通常,这就是你想要的

#1


2  

Impossible to use node.js script in your browser, start it with node.. :)

无法在浏览器中使用node.js脚本,请以节点启动.. :)

You can use jQuery for do that in client side :

您可以使用jQuery在客户端执行此操作:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script>
     console.log("just a check1");
     $.ajax({
         type:'GET',
         url:'./hello.txt',
         dataType: 'text',
      }).success(function (data){
               s = data.substr(32);
               var s2 = s.replace(/\r\n/g," ");
               var array = s2.split(' ');

               var col1 =[];
               var col2 =[];
               var col3 =[];
               var j=0;
               for(var i = 0; i <= array.length-3; i=i+3){
                       col1[j]=array[i];
                       col2[j]=array[i+1];
                       col3[j]=array[i+2];
                       j++;
               };

               console.log(col1,col2, col3);
      })
 </script>

</head>
<body>
  <h1>//hello world !</h1>
</body>
</html>

$.ajax allow you to get back datas with a specify url. Normally,that do that you wanted

$ .ajax允许您使用指定的URL返回数据。通常,这就是你想要的