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
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
add a comment |
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
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
add a comment |
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
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
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
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
node.js reactjs express
asked Nov 10 at 7:20
Faris Dewantoro
527
527
add a comment |
add a comment |
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
i want to validate other data not file data, i want validate this data : dataMurid
– Faris Dewantoro
Nov 11 at 12:31
add a comment |
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
i want to validate other data not file data, i want validate this data : dataMurid
– Faris Dewantoro
Nov 11 at 12:31
add a comment |
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
i want to validate other data not file data, i want validate this data : dataMurid
– Faris Dewantoro
Nov 11 at 12:31
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password