简体   繁体   中英

How to fix “document is not defined”

在此处输入图像描述 I'm getting an issue of 'document not found' in nodejs using with ejs. I want to read the file from client using "input type= file" and get the data of file to my server.

/ =======Nodejs Code:====== /

 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:====== /

<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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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