@Nmkou (kou Nm)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Node.js、HTMLで記述したプログラムが 'ReferenceError: document is not defined'エラーで動きません。

Node.js、HTMLで記述したプログラムが
'ReferenceError: document is not defined'エラーで動きません。

表題の件で質問です。

Node.js、HTMLを用いて
「習慣」「それに使った時間」を入力 → 週・月・年単位で予定消費時間を出力する
WEBアプリを作っているのですが、ターミナルでjsファイルを実行しようとすると下記のようなエラーが出てきてしまいます。

解決方法をご存じの方がいらっしゃいましたら、アドバイスをいただけませんでしょうか。

発生している問題・エラー

PS D:\code\Project1> node app.js
D:\code\Project1\app.js:12
    const habit = document.habit;
                  ^

ReferenceError: document is not defined
    at Object.<anonymous> (D:\code\Project1\app.js:12:19)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

該当するソースコード

入力ページHTML

<!DOCTYPE html>
  <head>
    <title>Life Time Manager</title>
    <link rel = "stylesheet" href="./stylesheet.css">

  </head>
  <body>
    <label for="name">習慣</label>
    <input type="text" id="habit" minlength="1" maxlength="10">

    <label for="name">時間</label>
    <input type="number" id="timeOnHabit" minlength="1" maxlength="10">

    <form name="form1">
      <select name="unit" size="1">
        <option value="day">/日</option>
        <option value="week">/週</option>
        <option value="month">/月</option>
        <option value="year">/年</option>
      </select>
    </form>

    <br>
    <a href="/views/result.ejs">
    <button type="submit" id="submit" >確認してみる</button>
    </a>

    <script src = "app.js"></script>
  </body>
</html>

出力ページHTML

<!DOCTYPE html>
  <head>
    <title>Life Time Manager</title>
    <link rel = "stylesheet" href="./stylesheet.css">
  </head>
  <body>
    <p>貴方は<%= habit %>をすることで</p>
    <p>1日あたり およそ<%= T %>時間</p>
    <p>1週間あたり およそ<%= T*7 %>時間</p>
    <p>1ヵ月あたり およそ<%= T*30 %>時間</p>
    <p>1年あたり およそ<%= T*365 %>時間</p>
    <p>を費やしています。</p>
  </body>
</html>

Javascript

const express = require('express');
const app = express();

  app.get('/top', (req, res)=>{
    res.render('top.ejs');
  });

  app.get('/result', (req, res)=>{
    res.render('result.ejs');
  });

    const habit = document.habit;
    const timeOnHabit = document.timeOnHabit;
    const unit = document.form1.unit;

      const T = (timeOnHabit, unit)=>{
        if(unit = day){
          return timeOnHabit;
        }
        if(unit = week){
          return timeOnHabit/7;
        }
        if(unit = month){
          return timeOnHabit/30;
        }
        if(unit = year){
          return timeOnHabit/365;
        }
      };


app.listen(3000);
0 likes

2Answer

document はブラウザで JavaScript を実行する際に使用可能なインターフェイスで、Node.js で実行する際には提供されないので未定義の変数になります。

HTML の <script> で app.js を読み込んでいたり、node で同じく app.js を直接実行したりと、サーバーサイドとクライアントサイドの処理を混同しているような印象を受けます。それぞれの JavaScript は似て非なるものですので、もう一度整理をしてみてください。

1Like

Comments

  1. @Nmkou

    Questioner

    ご回答いただきありがとうございます。

    >サーバーサイドとクライアントサイドの処理を混同しているような印象を受けます。それぞれの JavaScript は似て非なるものですので、もう一度整理をしてみてください。

    違和感の正体がわかりました。
    言語の役割をもう一度更ってみます。

document is not defined

documentが定義されていません、という内容のエラーです。
変数は予め宣言することで使用できるようになりますが、
documentは宣言されていないので、存在しないものを使おうとしている、ということになります。

const a = 1; // aを宣言
const b = a + 1; // aが使える

const c = d + 1; // dは宣言されていない(d is not defined)

おそらくフォームの値を処理したいのだと思うのですが、
コードを見たところ、基本的な部分をまだ習得されていないのかな、と感じます。

やりたいこと、を細かく分解して、ひとつひとつ解決していくのが良いと思います。

0Like

Comments

  1. @Nmkou

    Questioner

    ご回答いただきありがとうございます。

    >やりたいこと、を細かく分解して、ひとつひとつ解決していく
    この方針でもう一度進めてみたいと思います。

Your answer might help someone💌