Search code examples
javascriptnode.jsmulter

req.file.map is not a function


I keep getting the error req.file.map is not a function. This is the code below

router.post("/", upload.single("tutorial"), function(req, res){
var tutorial    = req.file;
    };
    var newCourse = {
        tutorial: `/course_uploads/${req.file.filename}`, 
    }
    Course.create(newCourse, function(err, newlyCreated){
        if(err){
            console.log(err)
        } else {
            //redirect it page back to the courses page
            res.redirect("courses/" + req.body.course);
        }
    });    
});

Solution

  • upload.single uploads only one file. If you think req.file is a array, well it's not. It's an object because you uploaded single file. Remove the map functions and write this for uploading single file.

    var tutorial = `/uploads/${req.file.filename}`