javascript - Mongoose model data not saved -
i working on nodejs project, using mongoose , mongodb, , when handling profile update logic, try post data server including profile photo upload, use formidable handle file upload , there no issue, other form fields not being saved there no error message, below it's route code, please me goes wrong.
router.post('/api/profileupdate/:username', function(req, res, next) { user.findone({ username: req.params.username }, function(err, user) { if (err) { console.log(err); } else { if (user) { console.log('user found, going update ...'); user.age = req.body.age; user.gender = req.body.gender; user.description = req.body.description; user.occupation = req.body.occupation; var form = new formidable.incomingform(); form.parse(req, function(err, fields, files) { //res.writehead(200, {'content-type': 'text/plain'}); //res.write('received upload:\n\n'); res.end(util.inspect({fields: fields, files: files})); }); form.on('end', function(fields, files) { console.log(fields); console.log(req.body.age); console.log(files); if (this.openedfiles[0]) { /* temporary location of our uploaded file */ var temp_path = this.openedfiles[0].path; /* file name of uploaded file */ var file_name = this.openedfiles[0].name; /* location want copy uploaded file */ var new_location = 'public/images/uploads/'; fs.copy(temp_path, new_location + file_name, function(err) { if (err) { console.error(err); } else { user.profile_photo = '/images/uploads/' + file_name + '?dim=200'; console.log(user.profile_photo); console.log("success!") // save data user.save(function(err) { if (err){ console.log('error in saving user: '+err); throw err; } console.log('user update succesful'); console.log(user.username); console.log(user.profile_photo); }); } }); } else { } }); res.redirect(req.url); } } }); });
try other approach see if files , fields coming through:
var fields=[] var files = [] form.on('field', function(field, value) { fields.push([field, value]); }) form.on('file', function(field, file) { console.log(file.name); files.push([field, file]); }) form.on('end', function() { console.log('done'); console.log(files) console.log(fields) });
Comments
Post a Comment