I have the following scenario;
- User requests files to be attached to a project
- Ctrl first looks to see that the user has access to the project
- If user has access, then allow the files to be uploaded and attached
- If something went wrong with the upload then return the error that was found
Code
projectService.getProject(ID)
.then(function (project) {
if (!project) {
return commonService.sendResponse(res, 404);
}
fileService.handleFileUpload(ID, [].concat(req.files.file))
.then(function (files) {
return commonService.sendResponse(res, 200, assets);
});
})
.catch(function (err) {
return commonService.sendError(res, err);
})
.finally(function cleanup() {
console.log('Fileupload completed');
});
Note: happy path flow works however the handleFileUpload can return an error if certain conditions are not met. If this method returns an error the catch returns the correct error but in the logs I see the following:
[TypeError: Cannot call method 'then' of undefined]
12:44:30.815 service [ERROR] Cannot call method 'then' of undefined (TypeError)
The line that it complains about is the following:
.then(function (files) {
The handleFileUpload looks like the following:
var handleFileUpload = function (projectId, fileList) {
var deferred = Promise.defer();
var reject = function (err) {
deferred.reject(err);
};
if (!(containsThumb(fileList)){
return reject(new Error('Missing thumbnail in request'));
}
/** some some logic **/
return deferred.promise;
};
}
exports.handleFileUpload = handleFileUpload;
Everything kinda works! But cant figure out the issue with the exception being logged.
Any help is appreciated.
J