Mongoose模型数据未保存

时间:2015-08-19 16:07:46

标签: javascript node.js mongodb model save

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

 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 个答案:

答案 0 :(得分:0)

尝试使用其他方法查看文件和字段是否全部通过:

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