javascript - "TypeError: Cannot read property ' filename ' of undefined "使用multer上传用户头像

标签 javascript node.js rest express multer

我正在尝试使用 express.js 服务器和 Multer 上传用户个人资料照片,但不知道为什么会出现“类型错误:无法读取未定义的属性‘文件名’”错误。 下面是我的服务器和 html 代码。

导入包

   const multer= require('multer');
   const path=require('path');
   const express=require('express');
   const app=express();
   app.use(express.json());
   app.use(cors()); 

中间件代码

      app.use(express.static( __dirname+"./backend/database/"));

      var upload=multer({dest:'public/'});

      var upload=multer({
    storage:multer.diskStorage({
           destination:function(req,file,cb){
              cb(null,'./public');
          },
            filename:(req,file,cb)=>{ cb(null,file.originalname);
         }
       })
    }).single('Image')

注册用户的API

    app.post('/register',upload,(req,res)=>{
         let userData ={
            Name:req.body.Name,
            Password:req.body.Password,
            Username:req.body.Username,
            ContactNo:req.body.ContactNo,
            Image:req.file.filename
         }
       console.log(req.body);//to check if image successfully recieved from the frontend  
     })

HTML代码

     <form [formGroup]="register" (ngSubmit)="Register(register.value)" enctype="multipart/form-data" >
            <legend>
                Sign Up
            </legend>
            <mat-form-field  color="warn">
                <mat-label>Name</mat-label>
                <mat-icon >person</mat-icon>
                <input matInput type="email" formControlName="Username" value="Username" />
            </mat-form-field>
    
            <mat-form-field  color="warn">
                <mat-label>Enter your Email</mat-label> 
                <mat-icon >email</mat-icon>
                <input matInput type="email" formControlName="Name" value="Email" />
            </mat-form-field>
            
            <mat-form-field  color="warn"> 
                <mat-label>Phone Number</mat-label>
                <mat-icon >call</mat-icon>
                <input matInput type="number"  formControlName="ContactNo" placeholder="+91 XXXXXXXXXX"
                    value="ContactNo" />
            </mat-form-field>
    
            <mat-form-field  color="warn">
                <mat-label>Write your Password</mat-label>
                <mat-icon >lock</mat-icon>
                <input matInput formControlName="Password" placeholder="Password" type="password" />
            </mat-form-field>

            <div color="warn">
                <label>Upload Your Photo</label>
                <mat-icon >photo</mat-icon>
                <input class="file"  formControlName="Image" placeholder="Upload Image" type="file" />
            </div>
            <button mat-raised-button color="warn" type="submit" (click)="changeAgain()">submit</button>
            <button mat-raised-button color="primary" style="margin-top: 2px;" type="reset">Reset</button>
            <br>
        </form>

发布数据

     Register(data)
    {
     this.serve.RegisterUser(data);
       //" serve " here is alias for injected HTTP services
    }

最佳答案

这是错误来源:

      Image:req.file.filename

因为你没有设置 multer 中间件。

app.use(multer({ storage: fileStorage, fileFilter }).single("image")); 

当你设置这个时,multer 会将'file'对象附加到“req”。目前req.file未定义

关于javascript - "TypeError: Cannot read property ' filename ' of undefined "使用multer上传用户头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65440907/

相关文章:

javascript - 安装谷歌标签管理器后如何修复网站性能?

javascript - Node.js Express 响应在 Promise 解析之前结束

javascript - Node JS函数执行顺序

spring - 如何模拟 REST 服务?

c# - 如何在 C# 中编写 REST Get-Request?

了解编码技术的 Javascript 书籍

javascript - 如果没有提交按钮,如何检查单选按钮是否已选中?

javascript - 为什么语句 if ( !condition ) { console.log(condition) } 显示 true

javascript - Node.js 多个 Express 对象

json - HTTP PATCH : Handling arrays, 删除和嵌套 key 创建