我在Loopback中有一个远程方法,该方法执行3个sql查询。我尝试使用multer将文件上传添加到此方法。我正在使用POSTMAN测试整个远程方法。当我为comedien_perso_nom
,comedien_perso_prenom
和nationalite
插入文件和一些文本并发送请求时,出现此错误:
TypeError : Cannot read property 'transfer-encoding' of undefined
可以肯定,它与multer有关,但不明白我在犯什么错误。同样对于插入photoscomedien
,我想在插入后插入文件的路径,但是我不知道如何。
这是我的远程方法:
'use strict';
var app = require('../../server/server.js');
var multer = require('multer');
var fs = require('fs');
module.exports = function(cm_comediens) {
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);
}
});
cm_comediens.setprofile1 = function (comedien_perso_nom,comedien_perso_prenom,nationalite,options,cb,req,res) {
// var userId = ctx.req.currentUser;
const token = options && options.accessToken;
const userId = token && token.userId;
var upload = multer({
storage: storage
}).array('file', 12);
upload(req, res, function (err) {
if (err) {
// An error occurred when uploading
res.json(err);
}
res.json(uploadedFileName);
});
var ds = app.datasources.mydb;
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,'" + fileName + "','" + fileName + "')";
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);
});
});
});
};
cm_comediens.remoteMethod(
'setprofile1',
{
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: 'req',type: 'object',http: {source: 'req'}},
{ arg: 'res',type: 'object',http: {source: 'res'}},
{ arg: "options", "type": "object", "http": "optionsFromRequest"},
// { arg: 'idComedien', type: 'number' }
],
returns: [{ arg: 'data', type: ['cm_comediens'], root: true },
{ arg: 'result',type: 'string'}]
}
);
}
任何帮助将不胜感激。谢谢