Javascript 使用未定义的busboy meanstack上载图像

Javascript 使用未定义的busboy meanstack上载图像,javascript,node.js,express,mean-stack,busboy,Javascript,Node.js,Express,Mean Stack,Busboy,我正在尝试制作一个带有图像上传功能的注册表单,因此我使用post和enctype=“multipart/form data”在ejs端获取值 每次尝试时都会显示此错误 TypeError:无法读取行上未定义的属性“on” req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType) 您能告诉我这个问题是关于什么的吗?在我看来,由于busboy不是中间件,它正在扩展请求,请参阅代码片段: var busboy

我正在尝试制作一个带有图像上传功能的注册表单,因此我使用post和enctype=“multipart/form data”在ejs端获取值

每次尝试时都会显示此错误
TypeError:无法读取行上未定义的属性“on”

req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType)

您能告诉我这个问题是关于什么的吗?

在我看来,由于busboy不是中间件,它正在扩展请求,请参阅代码片段:

var busboy = new Busboy({ headers: req.headers });
    busboy.on('file',...

我以前使用过busboy上传图像,我将与您分享配置及其实现方式

  • 导入busboy模块

    var-Busboy=需要(“Busboy”)

  • 然后在post端点中声明busboy对象

    var busboy = new Busboy({
      headers: req.headers,
      limits: {
        fileSize: 6*1024*1024 //2MB limit
      }
    });
    
  • 后跟post端点内的其余代码

    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
           //extract intput-field from upload-form
    });
    
    
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
          //process each file upload, check for size, mimetype, 
          //store if all checks passed, delete otherwise, move to 
          //next file
    });
    
    
    //finish call back when all files are uploaded
    busboy.on('finish', function() {
        //do cleanup and other related work 
    });
    
    return req.pipe(busboy);
    
  • 我在项目中创建了一个要点,我使用了busboy上传多张图片,并进行了以下检查

  • 从上传表单中提取输入字段
  • 确保文件大小未超过指定的XMB
  • 确保mimetype有效
  • 一旦发现超过XMB(X.1MB)的文件,则中止上载并移动到下一个文件
  • 如果文件满足所有检查,则将其存储在本地

  • 这里是公共要点的链接

    我改用multer,它工作正常


    on('字段',…)的作用是什么?这是一个回调函数,它将提取上传表单的输入字段,基本上是任何不是文件类型的字段(如firstName、lastName等)。我根据您的修改了我的模块。错误消失了,但它现在甚至没有收到字段(firstName等)。你认为我从html方面正确地处理了它吗?我编辑了我的问题代码,如果您想查看一下,您可以放置一些控制台日志以查看是否调用了on字段吗?如果调用了yes,那么调用它的字段名的值是多少。只要表单中的输入字段在文件字段之前,它就应该可以访问。现在它不再显示错误,但不会收到字段,我无法将它们保存在mongoose上
    var busboy = new Busboy({
      headers: req.headers,
      limits: {
        fileSize: 6*1024*1024 //2MB limit
      }
    });
    
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
           //extract intput-field from upload-form
    });
    
    
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
          //process each file upload, check for size, mimetype, 
          //store if all checks passed, delete otherwise, move to 
          //next file
    });
    
    
    //finish call back when all files are uploaded
    busboy.on('finish', function() {
        //do cleanup and other related work 
    });
    
    return req.pipe(busboy);