Express Multer在上传之前先验证字段

时间:2018-11-10 07:20:19

标签: node.js reactjs express

我已经看到了这样的内容:NodeJS Multer validate fields before upload,但没有用。我试过在几天内上传之前进行make validate字段验证,结果不起作用。是因为mutler无法做到这一点吗?

使用IERN的Iam:MongoDB,React,Express,Node

enter image description here

我想在上传图像之前在Form-data:dataMurid中验证此数据。 还有另一种方法可以克服这个问题吗?也许使用其他库?请给我示例代码。

路线

// @route buat edit/create murid baru
router.post('/datasiswa/create',(req, res,next) => {
    upload(req,res,(err)=>{

        let request = JSON.parse(req.body.newMurid);
        // upload(req.body.data.data, res, (err) => {
        //     console.log(req.body.data.data);
        // });
        const { errors, isValid } = validateMuridInput(request);

        // Check validation
        if (!isValid) {
            return res.status(400).json(errors);
        }
        const muridFields = {};
        if (request.tempatLahir) muridFields.tempatLahir = request.tempatLahir;
        if (request.jenisKelamin) muridFields.jenisKelamin = request.jenisKelamin;
        if (request.nis) muridFields.nis = request.nis;
        if (request.nama) muridFields.nama = request.nama;
        if (request.tanggalLahir) muridFields.tanggalLahir = request.tanggalLahir;
        if (request.namaAyah) muridFields.namaAyah = request.namaAyah;
        if (request.namaIbu) muridFields.namaIbu = request.namaIbu;
        if (request.noTelepon) muridFields.noTelepon = request.noTelepon;
        if (request.hpSiswa) muridFields.hpSiswa = request.hpSiswa;
        if (request.hpIbu) muridFields.hpIbu = request.hpIbu;
        if (request.hpAyah) muridFields.hpAyah = request.hpAyah;
        if (request.alamat) muridFields.alamat = request.alamat;

        Murid.findOne({ nis: request.nis })
            .then((murid) => {
                if (murid) {
                    errors.nis = 'NIS ini sudah terdaftar';
                    return res.status(400).json(errors);
                } else {

                    const newMurid = new Murid(muridFields);

                    newMurid.save()
                        .then((murid) => {
                            res.json(murid);
                        })
                        .catch((err) => {
                            console.log(err);
                        });


                }
            })
    });


});

上传功能

const multer = require('multer');
const path = require('path');

// UPLOAD IMAGE

// Set Storage engine
const storage = multer.diskStorage({
    destination: './public/uploads/',
    filename: function (req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
});

// Init upload 
let upload = multer({
    storage: storage,
    limits:{fileSize:1000000}, //file size dalam bit
}).fields([{ name: 'fotoDisplay' }, { name: 'newMurid' }]);

1 个答案:

答案 0 :(得分:0)

使用multer,在您的req.file文件中,您将拥有有关文件的所有字段。

var multer = require('multer')
    var upload = multer({ dest: 'uploads/' })
    import fs from 'fs-extra'

    router.post('/register', upload.single('avatar'), (req, res, next) => {
        return fs.readFile(req.file.path)
                .then(content => {
                    // The content of the file
                })
    }

req.file具有模仿类型,还有更多内容可供您检查