无法通过多重方法发回对象

问题描述:

我正在通过多重播放器执行文件上传,并且由于我想将文件存储在特定的位置,并将其命名为我自己的文件名,因此我正在使用destination以及创建存储对象时multer提供的filename属性。无法通过多重方法发回对象

我遇到的问题是我想将新创建的对象的信息存储在数据库中之后发回客户端。但是,没有res参数来做到这一点,我只能在我的post方法中做到这一点,它没有我刚刚创建的对象。

var storage = multer.diskStorage({ 
    destination: function (req, file, cb) { 
     cb(null, './uploads'); // Absolute path. Folder must exist, will not be created for you. 
    }, 
    filename: function (req, file, cb) { 
     var fileType = file.mimetype.split("/")[1]; 
     var fileDestination = file.originalname + '-' + Date.now() + "." + fileType; 

     cb(null, fileDestination); 

     var map = new Map({ 
      mapName: req.body.mapTitle, 
      mapImagePath: "./uploads/" + fileDestination, 
      ownerId: req.user._id 
     }); 

     Map.createMap(map, function(err, map){ 
      if(err) 
       next(err); 
      console.log(map); 
     }); 
    } 
}); 

var upload = multer({ storage: storage }); 

router.post('/', upload.single('mapImage'), function (req, res) { 

    res.status(200).send({ 
     code: 200, success: "Map Created." 
    }); 

}); 

Multer附加文件请求对象,你有你的post方法访问这些:

app.post('/', upload.single('mapImage'), function (req, res, next) { 
    console.log(req.file.filename); // prints the filename 
    console.log(req.file.destination); // prints the directory 
    console.log(req.file.path); // prints the full path (directory + filename) 
    console.log(req.file.originalname); // prints the name before you renamed it 
    console.log(req.file.size); // prints the size of the file (in bytes) 

    res.json(req.file); 
});