MULTER-S3:TypeError:无法将对象转换为原始值... diate [as _immediateCallback]

时间:2016-04-16 19:14:19

标签: javascript angularjs image-uploading mean-stack multer

我正在使用Multer-s3构建一个MEAN堆栈应用程序,以将图像上传到我的数据库。但是,当我调用上传函数时,我收到此错误:

POST http://localhost:3000/api/upload/single 500 (Internal Server Error)
TypeError: Cannot convert object to primitive valu…diate [as _immediateCallback](timers.js:354:15)↵", status: 500, config: Object, statusText: "Internal Server Error"

在我的服务器端app.js

    // MULTER
    var s3config = require('./config/s3');
    var upload = multer({
      storage: s3({
        dirname: s3config.dirname,
        bucket: s3config.bucket,
        secretAccessKey: process.env.AWS_ACCESS_SECRET,
        accessKeyId: process.env.AWS_ACCESS_KEY,
        region: s3config.region,
        contentType: function(req, file, next) {
          next(null, file.mimetype);
        },
        filename: function(req, file, next) {
          var ext = '.' + file.originalname.split('.').splice(-1)[0];
          var filename = uuid.v1() + ext;
          next(null, filename);
        }
      })
    });

    // UPLOAD SINGLE FILE
    app.post('/api/upload/single', upload.single('file'), function(req, res) {
      var clothing_1 = {
        type: req.body.type,
        image: req.file.key
      };
      console.log('req.body =' + req.body);
      console.log('req.body.type =' + req.body.type);
      console.log('req.file.key =' + req.file.key);
      // get the user model User.findOne({ id: req.user._id })
      User.findOne({ _id: req.user._id }, {}, { new: true }, function(err, user){
        user.clothing.push(clothing_1);
        // save this user
        user.save(function(err, user){
          if(err) return res.status(401).send({ message: 'your error:' + err });
          else return res.json({ user: user })
        });
      });

    });

在我的前端控制器中:

  self.uploadSingle = function() {
    Upload.upload({
      url: API + '/upload/single',
      data: { file: self.file, type: self.clothingType }
    })
    .then(function(res) {
        self.usersClothing = res.data.user.clothing.map(function(clothing) {
            clothing.image = S3_BUCKET + clothing.image;
            return clothing;
        });
    })
    .catch(function(err) {
      console.error(err);
    });
  }

按下上传按钮时执行此功能。

1 个答案:

答案 0 :(得分:0)

只是服务器无法找到用户的问题,因为req.body在_doc中提供了_id。

 User.findOne({ _id: req.user._doc._id } fixed the issue