猫鼬模型数据未保存 [英] Mongoose model data not saved

查看:102
本文介绍了猫鼬模型数据未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongoose和mongodb开发一个nodejs项目,并且在处理配置文件更新逻辑时,我尝试将一些数据(包括配置文件照片上传)发布到服务器,我使用强大的功能来处理文件上传,并且没问题,但是即使没有任何错误消息也没有保存我的其他表单字段,下面是路由代码,请帮助我哪里出错了.

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

这篇关于猫鼬模型数据未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆