【问题标题】:NodeJS files is not defined [duplicate]NodeJS文件未定义[重复]
【发布时间】:2018-06-06 09:17:43
【问题描述】:

我试图在页面上显示我已经上传的图像/(图像),但它说文件未定义。

app.get('/profile',(req,res) =>{
gfs.files.find().toArray((err, files) => {
    // Check if files
    if (!files || files.length === 0) {
      res.render('profile', {files: false})

    } else 
    {
        files.map(file => {
            if(file.contentType === 'image/jpeg' || file.contentType === 'image/png'){
                file.isImage = true;
            } else {
                file.isImage = false;
            }
        });
        res.render('profile',{files: files})
    }
  });

我正在使用 EJS 视图引擎。配置文件.ejs 我的代码:

  <div class="container">
        <div class="row">
          <div class="col-md-6 m-auto">
            <form action="/upload" method="POST" enctype="multipart/form-data">
              <div class="custom-file mb-3">
                <input type="file" name="file" id="file" class="custom-file-input">
                <label for="file" class="custom-file-label">Upload your image</label>
              </div>
              <input type="submit" value="Submit!" class="btn btn-primary btn-block">
            </form>
            <hr>
             <% if(files){ %>
              <% files.forEach(function(file) { %>
                <div class="card card-body mb-3">
                  <% if(file.isImage) { %>
                    <img src="image/<%= file.filename %>" alt="">
                    <% } else { %>
                      <%= file.filename %>
                        <% } %>  
                    </div>
                <% }) %>
                  <% } else { %>
                    <p>No files to show</p>
                    <% } %>
      </div>
  </div>

我不明白为什么我总是收到文件未定义的错误。 ReferenceError: C:\Users\User\Desktop\Oauth\views\profile.ejs:111

    109|         </form>

    110|         <hr>

    111|          <% if(files){ %>
files is not defined

【问题讨论】:

  • 如果将上面的内容更改为app.get('/profile',(req,res) =&gt; res.render('profile', {files: false}))会发生什么
  • 注意:files.map(file =&gt; { 应该是 files.forEach(file =&gt; { 因为 a) 你忽略了返回的值,b) 你改变了原来的“文件”对象,添加了 isImage 属性.不是这个问题,而是为了更清晰的代码,使用forEach而不是map
  • 请不要转发问题。而是解决现有问题中的问题。你在这里得到了很多与原版相同的反馈,正如那里提到的,问题很可能是“范围界定”。请返回原始内容并将其分解为一个小清单,该清单重现了问题,而不仅仅是大型应用程序的一部分。见How to create a Minimal, Complete, and Verifiable example

标签: javascript node.js mongodb ejs


【解决方案1】:

尝试检查回调中的 err 参数以开始调试。

app.get('/profile',(req,res) =>{
    gfs.files.find().toArray((err, files) => {

    if (err) {
        console.log(err); // debug the error
        return res.render('profile', {files: false});
    }

    // Check if files
    if (!files || files.length === 0) {
        res.render('profile', {files: false})
    } else {
        // map the files and set it to itself
        files = files.map((file) => {
            if(file.contentType == 'image/jpeg' || file.contentType == 'image/png'){
                file.isImage = true;
            } else {
                file.isImage = false;
            }
            return file; // You forgot this.
        });

        res.render('profile',{files: files})
    }
});

【讨论】:

  • 谢谢你,但它不起作用,不幸的是......
  • 你可以试试console.log(files)里面的回调吗?
  • 嗯...它什么也不显示
  • 考虑到代码在files.map 中修改了file,这基本上与OP 代码没有什么不同(OP 可以使用forEach 并且仍然得到相同的结果)
  • github.com/bradtraversy/mongo_file_uploads/blob/master/app.js 这是我试图用来学习如何上传文件并稍后在我的页面上显示的示例
猜你喜欢
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 2019-05-30
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多