Ошибка 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")); |
Цитата:
и что такое body и tblBody??? |
Не могли бы вы пояснить подробнее про мои ошибки(даже если они кажутся вам сверх глупыми)
|
kolfer,
getElementsByTagName - это список, нужен индекс var body = document.getElementsByTagName("body")[0]; tblBody это у вас никак не определено, можно здесь tbl.appendChild(row); строку 46 убрать, циклы for тоже немного странные у вас возможно красное лишнее j <= result.lenx, а второй for и вовсе не нужен. |
Просмотрел ваши замечания, исправил. Но это не решает моей главной проблемы ReferenceError: document is not defined.
И что с этим делать я не имею ни малейшего понятия. |
kolfer,
ждите специалиста по Node.JS, может там нет никакого document, а как то иначе устроено... |
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, время: 07:00. |