Closed jbeltrami closed 7 years ago
take a look at your route for the uploads controller. Is it creating a route for all restful actions?
It is getting into the action I want in the controller. I'm trying to get to create. The problem seems to be with the body of the object I'm passing in with req.
The solution was to set multer as a before action and fix the owner of the new upload. All changes made to the controller that now looks like this:
'use strict';
const controller = require('lib/wiring/controller');
const models = require('app/models');
const Upload = models.upload;
const multer = require('multer')
const multerUpload = multer({ dest: '/tmp/' })
const authenticate = require('./concerns/authenticate');
const setUser = require('./concerns/set-current-user');
const setModel = require('./concerns/set-mongoose-model');
const s3Upload = require('lib/aws-s3-upload')
const index = (req, res, next) => {
console.log('I\'m getting in index.')
Upload.find()
.then(uploads => res.json({
uploads: uploads.map((e) =>
e.toJSON({ virtuals: true, user: req.user })),
}))
.catch(next);
};
const show = (req, res) => {
console.log('I\'m getting in show.')
res.json({
upload: req.upload.toJSON({ virtuals: true, user: req.user }),
});
};
const create = (req, res, next) => {
const file = {
path: req.file.path,
name: req.body.file.name,
originalname: req.file.originalname,
mimetype: req.file.mimetype
}
s3Upload(file)
.then((s3Response) => {
return Upload.create({
url: s3Response.Location,
name: file.name,
_owner: req.user._id,
originalname: req.file.originalname,
mimetype: req.file.mimetype
})
})
.catch((error) => {console.error})
};
const update = (req, res, next) => {
console.log('I\'m getting in update.')
delete req.body._owner; // disallow owner reassignment.
req.upload.update(req.body.upload)
.then(() => res.sendStatus(204))
.catch(next);
};
const destroy = (req, res, next) => {
console.log('I\'m getting in destroy.')
req.upload.remove()
.then(() => res.sendStatus(204))
.catch(next);
};
module.exports = controller({
index,
show,
create,
update,
destroy,
}, { before: [
{method: multerUpload.single('file[path]'), only: ['create'] },
{ method: setUser, only: ['index', 'show'] },
{ method: authenticate, except: ['index', 'show'] },
{ method: setModel(Upload), only: ['show'] },
{ method: setModel(Upload, { forUser: true }), only: ['update', 'destroy'] },
], });
We're trying to make an upload to S3, using the request body to pass in data. But our AJAX request is passing an empty body to our controller.
This is the event that creates the form:
This is how the HTML looks like:
This is the API call:
This is my route on the server side:
And this is how the controller is handling that request:
I'm getting a 500 error from the server and the script never gets to the line of creating upload (on the controller) @pjliddy @prankmode @eliottenos