无法读取属性文件名multer和节点错误

时间:2018-07-17 10:59:42

标签: node.js multer

我刚刚开始构建将multer与node js结合使用的示例。我建立了一个单独的项目,该项目实际上运行良好,但是当我在相同的设置上进行复制时,对从app.get到router.get之类的路由进行了微小更改,就出现了错误:

TypeError: Cannot read property 'filename' of undefined

这是我的路线代码:

router.post('/userprofilepic', ensureAuthenticated, (req, res) => {

    upload(req, res, (err) => {
        if (err) {
            res.render('/user/userdashboard', {
                msg: err
            });
        } else {

            const newImage = {
                imageName: req.file.filename,
                image_caption: req.body.image_caption,
                member_id: req.user.member_id
            }
            new Image(newImage)
                .save()
                .then(image => {

                    req.flash('success_msg', 'Image added');
                    res.redirect('/user/userdashboard');
                })
                .then(resize => {
                    let imgHeight = 150;
                    let imgWidth = 150;
                    sharp(req.file.path)
                        .blur(.3)
                        .toFile('public/thumbs/blurred_' + req.file.originalname, function (err) {
                            if (!err) {
                                req.flash('success_msg', 'Thumbnail created');
                                //res.redirect('/');
                            }
                        });
                })
                .catch(error => {
                    console.log(error);
                });


        }
    });
});

这是我的multer配置:

// Set storage engine
const storage = multer.diskStorage({
    destination: './public/uploads/',
    filename: function (req, file, cb) {
        cb(null, req.user.member_id + '_' + Date.now() + '_' + file.originalname);
    }
});



// Init upload
const upload = multer({
    storage: storage
}).single('imageName');



//load user model
require('../models/User');
const User = mongoose.model('users');

//load user profile model
require('../models/Profile');
const Profile = mongoose.model('profiles');

// Load images model
require('../models/Images');
const Image = mongoose.model('images');

最后是我的表格:

<form action="/user/userprofilepic" method="POST" enctype="multipart/form-data">
                        <div class="form-group">
                            <img id="blah" src="#" alt="your image" height="300" />
                        </div>
                        <div class="form-group">
                            <label for="exampleFormControlFile1">Example file input</label>
                            <input type="file" class="form-control-file" name="imageName" id="imageName">
                        </div>

                        <div class="form-group">
                            <label for="exampleInputEmail1">Image Caption</label>
                            <input type="text" class="form-control" name="image_caption">
                            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                        </div>

                        <div class="form-group">

                            <input type="text" class="form-control" name="member_id" value="{{user.member_id}}" hidden>

                        </div>

                        <button type="submit" class="btn btn-primary">Submit</button>
                    </form>

我尝试了console.log(req.file),它是未定义的。对于我的一生,我无法理解我做错了什么。

请告知。

1 个答案:

答案 0 :(得分:0)

弄清楚了...这是我的错误,在我的服务器上。 js我初始化了一个上载方法,该方法将覆盖multer上载功能,因此没有文件名。