首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MULTER-S3: TypeError:无法将对象转换为原始值…diate [作为_immediateCallback]

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

Stack Overflow用户
提问于 2016-04-17 03:14:19
回答 1查看 223关注 0票数 1

我正在构建一个使用Multer-s3将图像上传到我的数据库的均值堆栈应用程序。然而,当我调用upload函数时,我得到了这个错误:

代码语言:javascript
复制
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中

代码语言:javascript
复制
    // 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 })
        });
      });

    });

在我的前端控制器中:

代码语言:javascript
复制
  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);
    });
  }

此函数在我按下upload按钮时执行。

EN

回答 1

Stack Overflow用户

发布于 2016-04-17 04:34:59

仅仅是服务器无法找到用户的问题,就像req.body在_doc中提供的_id一样。

代码语言:javascript
复制
 User.findOne({ _id: req.user._doc._id } fixed the issue
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36668552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档