简体   繁体   English

如何修复“文档未定义”

[英]How to fix “document is not defined”

在此处输入图像描述 I'm getting an issue of 'document not found' in nodejs using with ejs.我在使用 ejs 的 nodejs 中遇到“找不到文档”的问题。 I want to read the file from client using "input type= file" and get the data of file to my server.我想使用“输入类型 = 文件”从客户端读取文件并将文件数据获取到我的服务器。

/ =======Nodejs Code:====== / / =======Nodejs代码:====== /

 router.get('/upload',(req,res)=>{

      const fileData= document.getElementById('uploadForm').files[0].path;
              fs.readFile(fileData,(err,data)=>{
                  if(err) throw err;
                 // let dataObject =JSON.parse(data);
                  console.log(data);
              });
       });

/ ======Ejs Code:====== / / ======Ejs代码:====== /

<form  action="/upload" method="POST">
                        <div class="col-md-6">
                              Upload License File (XLS / TXT): <input type="file"  id="uploadForm" name="uploadForm" >
                              <button type="submit"  class="btn btn-primary" style="margin-top: 10px;border-radius:0;">Upload</button>

                        </div>
                  </form>

  <script type="text/javascript" src="/routes/index.js"> 
                                          var x = document.getElementById('uploadForm'); //make sure this tag is below "demo"
                                    </script>  




ReferenceError: document is not defined
    at Object.<anonymous> (C:\Users\Pranay\Desktop\nodejs_Lic\routes\index.js:101:14)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\Pranay\Desktop\nodejs_Lic\app.js:7:19)
    at Module._compile (internal/modules/cjs/loader.js:701:30)

Your code doesn't have a problem, you just forgot an = sign after your constant fileData .您的代码没有问题,您只是在常量fileData之后忘记了一个=符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM