node.js - Express Multer 在上传前验证字段

标签 node.js reactjs express

我见过这个:NodeJS Multer validate fields before upload但这不是工作。我在几天内尝试在上传之前进行验证字段,但结果不起作用。是因为穆勒做不到吗?

我正在使用 MERN:MongoDB、React、Express、Node

enter image description here

我想在上传图像之前验证表单数据: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' }]);

最佳答案

使用 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 包含 mimetype 以及更多内容供您检查

关于node.js - Express Multer 在上传前验证字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53236852/

相关文章:

javascript - 将外观输入字段更改为日期选择器中的按钮

node.js - cors 无法在带有 Express 的 firebase 函数中工作

node.js - centos服务器无法运行node express

node.js - express/connect 中间件的控制顺序

node.js - 在 Flow NPM 包中,抑制问题以使用户应用程序看不到错误的正确方法是什么?

javascript - 我正在尝试从 Node JS/Express 连接到 postgres 数据库。 postman 没有回应

javascript - 如何在使用 webpack 捆绑 React 时禁用严格模式

python - Node : Using child process in aws lambda to run python script not working

image-processing - 在 node.js 中调整图像大小

ios - IOS 设备的反向 TCP?