Mongoose model data not saved

≡放荡痞女 提交于 2020-01-06 18:34:02

问题


I am working on a nodejs project, using mongoose and mongodb, and when handling a profile update logic, I try to post some data to the server including a profile photo upload, I use formidable to handle the file upload and there is no issue, but my other form fields not being saved even there is no any error message, below it's the route code, please help me where 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 to 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;
                        /* The file name of the uploaded file */
                        var file_name = this.openedFiles[0].name;
                        /* Location where we want to copy the 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 the 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);

            }
        }
 });
});

回答1:


Try this other approach to see if the files and fields are all 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)
});


来源:https://stackoverflow.com/questions/32100641/mongoose-model-data-not-saved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!