简体   繁体   中英

Mongoose model data not saved

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);

            }
        }
 });
});

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)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM