Express Multer validate fields before upload









up vote
0
down vote

favorite












I have been seen this : NodeJS Multer validate fields before upload but is not work. i tried for make validate field before upload in a days and the result is not working. is it because mutler can't do this?



Iam using MERN : MongoDB,React,Express,Node



enter image description here



i want validate this data in Form-data : dataMurid before uploading the image.
is there another way to overcome this? Maybe using other library? and please give me exampale code.



Route



// @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);
);



)
);


);


Upload func



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' ]);









share|improve this question

























    up vote
    0
    down vote

    favorite












    I have been seen this : NodeJS Multer validate fields before upload but is not work. i tried for make validate field before upload in a days and the result is not working. is it because mutler can't do this?



    Iam using MERN : MongoDB,React,Express,Node



    enter image description here



    i want validate this data in Form-data : dataMurid before uploading the image.
    is there another way to overcome this? Maybe using other library? and please give me exampale code.



    Route



    // @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);
    );



    )
    );


    );


    Upload func



    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' ]);









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have been seen this : NodeJS Multer validate fields before upload but is not work. i tried for make validate field before upload in a days and the result is not working. is it because mutler can't do this?



      Iam using MERN : MongoDB,React,Express,Node



      enter image description here



      i want validate this data in Form-data : dataMurid before uploading the image.
      is there another way to overcome this? Maybe using other library? and please give me exampale code.



      Route



      // @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);
      );



      )
      );


      );


      Upload func



      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' ]);









      share|improve this question













      I have been seen this : NodeJS Multer validate fields before upload but is not work. i tried for make validate field before upload in a days and the result is not working. is it because mutler can't do this?



      Iam using MERN : MongoDB,React,Express,Node



      enter image description here



      i want validate this data in Form-data : dataMurid before uploading the image.
      is there another way to overcome this? Maybe using other library? and please give me exampale code.



      Route



      // @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);
      );



      )
      );


      );


      Upload func



      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' ]);






      node.js reactjs express






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 7:20









      Faris Dewantoro

      527




      527






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          With multer, in your req.file, you have all the fields about the 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 has the mimetype and so much more that you can check






          share|improve this answer




















          • i want to validate other data not file data, i want validate this data : dataMurid
            – Faris Dewantoro
            Nov 11 at 12:31










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236852%2fexpress-multer-validate-fields-before-upload%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          With multer, in your req.file, you have all the fields about the 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 has the mimetype and so much more that you can check






          share|improve this answer




















          • i want to validate other data not file data, i want validate this data : dataMurid
            – Faris Dewantoro
            Nov 11 at 12:31














          up vote
          0
          down vote













          With multer, in your req.file, you have all the fields about the 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 has the mimetype and so much more that you can check






          share|improve this answer




















          • i want to validate other data not file data, i want validate this data : dataMurid
            – Faris Dewantoro
            Nov 11 at 12:31












          up vote
          0
          down vote










          up vote
          0
          down vote









          With multer, in your req.file, you have all the fields about the 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 has the mimetype and so much more that you can check






          share|improve this answer












          With multer, in your req.file, you have all the fields about the 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 has the mimetype and so much more that you can check







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 13:33









          Pedro Silva

          35910




          35910











          • i want to validate other data not file data, i want validate this data : dataMurid
            – Faris Dewantoro
            Nov 11 at 12:31
















          • i want to validate other data not file data, i want validate this data : dataMurid
            – Faris Dewantoro
            Nov 11 at 12:31















          i want to validate other data not file data, i want validate this data : dataMurid
          – Faris Dewantoro
          Nov 11 at 12:31




          i want to validate other data not file data, i want validate this data : dataMurid
          – Faris Dewantoro
          Nov 11 at 12:31

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236852%2fexpress-multer-validate-fields-before-upload%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線