在Node.js / Loopback中上传文件无法与Multer一起使用

时间:2019-05-06 10:37:18

标签: javascript node.js file-upload loopbackjs multer

使用回送,我有一个执行3个SQL查询的远程方法。我想添加文件上传,但是有一个问题:文件未上传,我正在使用multer进行上传。实际上,当我想使用POSTMAN进行测试时,它正确执行且没有错误,除了文件上传不起作用,因此图像未复制到提到的文件夹中,并且第三个查询中路径的插入在数据库中显示为未定义(显而易见,因为没有文件上传时找不到路径)。我没有发现自己的错误,我们将不胜感激。谢谢

这是我的远程方法:

 module.exports = function(cm_comediens) {

   cm_comediens.upload = function (comedien_perso_nom,comedien_perso_prenom,nationalite,options,req,res,cb) {
      const token = options && options.accessToken;
      const userId = token && token.userId;
      var ds = app.datasources.mydb;

      var uploadedFileName = '';
      var storage = multer.diskStorage({
        destination: function (req, file, cb) {
          // checking and creating uploads folder where files will be uploaded
          var dirPath = 'server/storage/common'
          if (!fs.existsSync(dirPath)) {
              var dir = fs.mkdirSync(dirPath);
          }
          cb(null, dirPath + '/');
      },
      filename: function (req, file, cb) {
          // file will be accessible in `file` variable
          var ext = file.originalname.substring(file.originalname.lastIndexOf("."));
          var fileName = Date.now() + ext;
          uploadedFileName = fileName;
          cb(null, fileName);
                }
      });
      var upload = multer({storage: storage}).single('photoscomedien');
      upload(req, res, function (err) {
      if (err) {res.json(err);}

      var sql = "UPDATE cm_comediens SET comedien_perso_nom=?,comedien_perso_prenom=?,nationalite=? WHERE id_comedien IN ( SELECT id_comedien FROM (SELECT id_comedien FROM cm_comediens WHERE id_utilisateur= '" + userId + "') as MakeitWork) ";
      var sql2 = "UPDATE photoscomedien SET photo_profile=0 WHERE id_comedien IN ( SELECT id_comedien FROM (SELECT id_comedien FROM cm_comediens WHERE id_utilisateur= '" + userId + "') as MakeitWork2)";
      var sql3 = "INSERT INTO photoscomedien (id_comedien,photo_profile,path,name) VALUES ((SELECT id_comedien FROM cm_comediens WHERE id_utilisateur= '" + userId + "'),1,'" + req.file.path + "','" + req.file.originalname + "')";

      ds.connector.execute(sql, [comedien_perso_nom,comedien_perso_prenom,nationalite], function (err, cm_comedienss) {
      ds.connector.execute(sql2, [], function (err, cm_comedienss) {
      ds.connector.execute(sql3, [], function (err, cm_comedienss) {
          if (err) console.error(err);
          cb(err,[cm_comedienss,uploadedFileName]); 
      });
      });
      });      
      });     

  };
  cm_comediens.remoteMethod(
      'upload',
      {
          http: { verb: 'PUT' },
          description: 'Update Comediens Infos Mon Profil Bloc 1 et Update photos',
           accepts:[
           { arg: 'comedien_perso_nom', type: 'string' },
           { arg: 'comedien_perso_prenom', type: 'string' }, 
           { arg: 'nationalite', type: 'string' } ,
           { arg: "options", "type": "object", "http": "optionsFromRequest"},
           { arg: 'req',type: 'object',http: {source: 'req'}},
           { arg: 'res',type: 'object',http: {source: 'res'}},
          ],
          returns: {arg: 'result',type: 'string',root: true}
      }
  );    }

0 个答案:

没有答案