sails.js req.file.upload with bluebird promise -


i'm using sails.js backend bluebird promise, tried upload file using req.file.upload, callback way works fine , file gets uploaded:

req.file('file').upload({     maxbytes: 2000000,     dirname: 'uploadfolder' }, function (err, files) {     if (err) {         return res.servererror(err);     }     return res.ok(); } 

but promise way not:

var promise = require('bluebird'); var fileuploader = promise.promisify(req.file('file').upload); fileuploader({     maxbytes: 2000000,     dirname: currentuploadfolder }).then(function(files) {     console.log(files);     return promise.resolve(files); }).catch(function(err) {     console.log(err);     return promise.reject(err); }); 

file doesn't uploaded , console keeps complaining:

data:    app.js:1800 - [typeerror: cannot read property 'length' of undefined] 

any thoughts please?

this case unique , can't use promisify here. need create new promise , return it.

return new promise(function(resolve, reject) {   req.file('file').upload({     maxbytes: 2000000,     dirname: 'uploadfolder'   }, function (error, files) {     return error ? reject(error) : resolve(files);   } }); 

you can move helper\services\whatever , got this

// api/services/uploadservice.js module.exports = {   uploadfile: function(req) {     return new promise(function(resolve, reject) {       req.file('file').upload({         maxbytes: 2000000,         dirname: 'uploadfolder'       }, function (error, files) {         return error ? reject(error) : resolve(files);       }     });   } };  // api/controllers/anycontroller.js module.exports = {   upload: function(req, res) {     uploadservice       .uploadfile(req)       .then(res.ok)       .catch(res.negotiate);   } }; 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -