How to add location path to Mysql DB with using Multer in Node.JS -
i'm new in node.js development. i've struggling problem: want upload photo in "/uploads" directory in project. i've added photo alone. when come adding photo server , save location path photos owner id , description mysql db didn't make it. know multer accept multipart-form data.
here node.js code
var models = require('../../models'); var express = require('express'); var router = express.router(); var multer = require('multer'); var app = express(); var bodyparser = require('body-parser'); app.use(bodyparser.json({limit:'50mb'})); app.use(bodyparser.urlencoded({extended:true, limit:'50mb'})); const uuidv1 = require('uuid/v1'); var owner_id; var description; var storage = multer.diskstorage({ destination: function (req, file, cb) { cb(null, 'uploads/') }, filename: function (req, file, cb) { const photo_id = uuidv1(); cb(null, photo_id + '.jpg'); addphototodb(photo_id,owner_id,description); } }); function addphototodb(photo_id,owner_id,description) { models.photos.create({ photo_id: photo_id, description: description, owner_id: owner_id, location_path: 'uploads/' + photo_id + '.jpg' }) } var upload = multer({ storage: storage }).single('photo'); router.post('/upload', function (req, res) { //this part problematic tried many things //owner_id=req.files //description=req.files upload(req, res, function (err) { if (err) { } res.json({ success: true, message: 'image uploaded!' }); }) }); module.exports = router; also in postman i'm sending request that: https://i.stack.imgur.com/04qz1.png
in screenshot show "body" tab. in left there "headers" tab. click on , check have content-type set "multipart/form-data" because multer won't process data aren't on content type.
Comments
Post a Comment