Javascript-форум (https://javascript.ru/forum/)
-   Node.JS (https://javascript.ru/forum/node-js-io-js/)
-   -   Ошибка document is not defined (https://javascript.ru/forum/node-js-io-js/71770-oshibka-document-not-defined.html)

kolfer 11.12.2017 13:14

Ошибка document is not defined
 
Пытаюсь генерить таблицу,но возникает вот такая лабуда. Кто знает, как это лечить?
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var mysql = require('mysql');
var http=require('http');
var urlencodedParser = bodyParser.urlencoded({extended: false});

app.use(express.static(__dirname + "/public"));

app.post("/evaluation", urlencodedParser, function (request, response) {
    if (!request.body) return response.sendStatus(400);
    console.log(request.body);
    year = request.body.year;
    section = request.body.section;
    var con = mysql.createConnection({
        host: "localhost",
        user: "Professor",
        password: "Professor123",
        database: "cotc"
    });
    con.connect(function (err) {
        if (err) throw err;
        con.query("select theory.Surname,theory.Name from theory,custumers where ((theory.id=custumers.id) and (custumers.Section='" + section + "')) and(custumers.Year='" + year + "')",
            function (err, result, fields) {
                console.log(result);
                console.log(result.length);
                response.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
                /*var i=0;
                 while (i < result.length) {
                 response.end( result[i].Name+' '+result[i].Surname);
                 i++;*/
                function tableCreate(x, lenx) {
                    var body = document.getElementsByTagName("body");
                    var tbl = document.createElement("table");
                    for (var j = 0; j <= result.lenx; j++) {
                        var row = document.createElement("tr");
                        for (var i = 0; i < 1; i++) {
                            var cell = document.createElement("td");
                            var cellText = document.createTextNode(result[j].Name);
                            cell.appendChild(cellText);
                            row.appendChild(cell);
                        }

                        tblBody.appendChild(row);
                    }
                    tbl.appendChild(tblBody);
                    body.appendChild(tbl);
                    tbl.setAttribute("border", "2");
                }
                tableCreate(result, result.length);
            });
    });
})

app.listen(8081,console.log("8081"));

рони 11.12.2017 13:25

Цитата:

Сообщение от kolfer
var body = document.getElementsByTagName("body");

??? kolfer,
и что такое body и tblBody???

kolfer 11.12.2017 16:17

Не могли бы вы пояснить подробнее про мои ошибки(даже если они кажутся вам сверх глупыми)

рони 11.12.2017 16:35

kolfer,
getElementsByTagName - это список, нужен индекс
var body = document.getElementsByTagName("body")[0];

tblBody это у вас никак не определено, можно здесь

tblBody.appendChild(row); заменить на
tbl.appendChild(row);


строку 46 убрать, циклы for тоже немного странные у вас
возможно красное лишнее j <= result.lenx,
а второй for и вовсе не нужен.

kolfer 11.12.2017 17:33

Просмотрел ваши замечания, исправил. Но это не решает моей главной проблемы ReferenceError: document is not defined.
И что с этим делать я не имею ни малейшего понятия.

рони 11.12.2017 17:40

kolfer,
ждите специалиста по Node.JS, может там нет никакого document, а как то иначе устроено...

рони 11.12.2017 17:56

kolfer,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <style type="text/css">
 </style>

  <script>
window.addEventListener("DOMContentLoaded", function() {
var result = [{Name: "a", Surname : "aa"},{Name: "b", Surname : "bb"},{Name: "d", Surname : "dd"}];
function tableCreate(result) {
                    var body = document.getElementsByTagName("body")[0];
                    var tbl = document.createElement("table");
                    for (var j = 0; j < result.length; j++) {
                        var row = document.createElement("tr");
                        var cell = document.createElement("td");
                        var cellText = document.createTextNode(result[j].Name);
                            cell.appendChild(cellText);
                            row.appendChild(cell);
                            tbl.appendChild(row);
                    }
                    body.appendChild(tbl);
                    tbl.setAttribute("border", "2");
                }
                tableCreate(result);
});
  </script>
</head>

<body>
</body>
</html>


Часовой пояс GMT +3, время: 21:19.