获取未定义的变量,尝试使用angular-file-upload和MEAN.js更新MongoDB中上传文件的路径

时间:2016-07-27 16:36:37

标签: mongodb meanjs angular-file-upload

如果有人可以帮助我,我会永远感激。几个星期以来,我一直在用砖墙砸我的头,试图通过MEAN.js用户模块将图像上传到开箱即用的方式上传。在生成的用户模块中,文件被上载到目录中,并且该文件的路径存储在mongodb文档的字段中。我可以使用multer和fileupload函数将文件上传到需要的位置。但是,我无法保存文档中的字段路径。我无法弄清楚如何避免获得'undefined'变量。我已经尝试创建一个$ window服务并将数据作为全局变量和其他一些东西传递给它,我完全陷入困境。

我已经评论了下面的代码,以演示我的服务器控制器changeShoePicture函数出错的地方。

// This is the boilerplate code from the mean.js "users" module.
// I can not create a $window service or global variable to store the 
// shoe data below so that I can update the shoe.shoeImageURL field
// in MongoDB  with path to the successfully uploaded file. 


exports.changeShoePicture = function (req, res) {

var message = null;
var shoe = req.shoe;
var upload = multer(config.uploads.shoeUpload).single('newProfilePicture');
var profileUploadFileFilter = require(path.resolve('./config/lib/multer')).profileUploadFileFilter;

console.log('i am here', shoe); // shoe is defined here.

// Filtering to upload only images. This works and proceeds to the else condition!

upload.fileFilter = profileUploadFileFilter;
upload(req, res, function (uploadError) {   

    if(uploadError) {
    return res.status(400).send({
      message: 'Error occurred while uploading profile picture'
    });
  } else {

 //shoe image file is successfully uploaded to the location on the server,      
// However the following fails because the shoe variable is undefined.   

shoe.shoeImageURL = config.uploads.shoeUpload.dest + req.file.filename;  
      }
  });

1 个答案:

答案 0 :(得分:0)

确保我做对了:

正在通过您的路线reqres传递的参数调用上传功能。您从shoe设置了req.shoe var。

upload()有什么机会搞乱你的请求?

在您调用上传并报告回来后立即放入console.log(req)