Просмотр исходного кода

Fix: error on upload attachment with create page

* Error was `Warning: a promise was created in a  handler but was not returned from it`
Sotaro KARASAWA 10 лет назад
Родитель
Сommit
1452439df4
1 измененных файлов с 12 добавлено и 29 удалено
  1. 12 29
      lib/routes/attachment.js

+ 12 - 29
lib/routes/attachment.js

@@ -78,25 +78,16 @@ module.exports = function(crowi, app) {
         fileName = tmpFile.name,
         fileType = tmpFile.mimetype,
         fileSize = tmpFile.size,
-        filePath = Attachment.createAttachmentFilePath(id, fileName, fileType);
+        filePath = Attachment.createAttachmentFilePath(id, fileName, fileType),
+        tmpFileStream = fs.createReadStream(tmpPath, {flags: 'r', encoding: null, fd: null, mode: '0666', autoClose: true });
 
-      fileUploader.uploadFile(
-        filePath,
-        fileType,
-        fs.createReadStream(tmpPath, {
-          flags: 'r',
-          encoding: null,
-          fd: null,
-          mode: '0666',
-          autoClose: true
-        }),
-        {}
-      ).then(function(data) {
-        debug('Uploaded data is: ', data);
-
-        // TODO size
-        Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize)
+      return fileUploader.uploadFile(filePath, fileType, tmpFileStream, {})
         .then(function(data) {
+          debug('Uploaded data is: ', data);
+
+          // TODO size
+          return Attachment.create(id, req.user, filePath, originalName, fileName, fileType, fileSize);
+        }).then(function(data) {
           var imageUrl = fileUploader.generateS3FileUrl(data.filePath);
           return res.json({
             status: true,
@@ -106,14 +97,13 @@ module.exports = function(crowi, app) {
             pageCreated: pageCreated,
             message: 'Successfully uploaded.',
           });
-        }, function (err) {
+        }).catch(function (err) {
           debug('Error on saving attachment data', err);
-
           // @TODO
           // Remove from S3
           return res.json({
             status: false,
-            message: '',
+            message: 'Error while uploading.',
           });
         }).finally(function() {
           fs.unlink(tmpPath, function (err) {
@@ -121,15 +111,8 @@ module.exports = function(crowi, app) {
               debug('Error while deleting tmp file.');
             }
           });
-        });
-      }, function(err) {
-        debug('Upload error to S3.', err);
-
-        return res.json({
-          status: false,
-          message: 'Error while uploading.',
-        });
-      });
+        })
+      ;
     });
   };