node.js multer saving file name into db -


i'm trying save uploaded file name db? thanks. how can pass multer file.name database. attempt using var: var fileimage = file.name;

router.use(multer({ // https://github.com/expressjs/multer   dest: './uploads/',    limits : { filesize:100000 },   rename: function (fieldname, filename) {     return filename.replace(/\w+/g, '-').tolowercase();   },   onfileuploaddata: function (file, data, req, res) {     // file : { fieldname, originalname, name, encoding, mimetype, path, extension, size, truncated, buffer }     var params = {       bucket: 'lemybucket01',       key: file.name,       body: data     };      s3.putobject(params, function (perr, pres) {       if (perr) {         console.log("error uploading data: ", perr);       } else {         console.log("successfully uploaded data mybucket/mykey");       }     });   },   onfileuploadcomplete: function (file) {     var fileimage = file.name;   } }));  router.route('/')     //post new prod     .post(function(req, res) {          if(req.files.fileimage !== undefined){ // `image` field name form             //res.redirect("/uploads"); // success         }else{             res.send("error, no file chosen");         }          // values post request. these can done through forms or rest calls. these rely on "name" attributes forms          var username = req.body.username;          //call create function our database         mongoose.model('prod').create({             username : username,             fileimage : fileimage         }, function (err, prod) {               if (err) {                   res.send("there problem adding information database.");               } else {                   //prod has been created                   console.log('post creating new prod: ' + prod);                   res.format({                       //html response set location , redirect home page. create 'success' page if that's thing                     html: function(){                         // if worked, set header address bar doesn't still /adduser                         res.location("prods");                         // , forward success page                         res.redirect("/prods");                     },                     //json response show newly created prod                     json: function(){                         res.json(bloprodb);                     }                 });               }         })     }); 

the req.files.fileimage !== undefined false?

the variable fileimage declared in following has limited scope

onfileuploadcomplete: function (file) {   var fileimage = file.name; } 

if want pass data(like fileimage) 1 middleware can use this.

onfileuploadcomplete: function (file, req, res) {   var fileimage = file.name;   req.middlewarestorage = {     fileimage : fileimage//,     //otherkey : othervalue   } } 

and can save in db as

var fileimage = req.middlewarestorage.fileimage; mongoose.model('prod').create({     username : username,     fileimage : fileimage }, function (err, prod) {   // stuff }); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -